Vue.extend() 传入选项对象 返回构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| let myExtend = Vue.extend({ data(){ return{ name:'jessica' } }, created(){ this.say() }, methods: { say(){ console.log('hello guys. say by'+this.name) } } }) let myComp = new myExtend()
|
Vue.component(‘component-name’,{options}) 全局注册子组件
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Vue.component('my-component',{ data(){ return { name:'zeng' } }, template:`<span> to {{name}} </span>`, }) let vm = new Vue({ el:'#vm' })
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| / Vue.component('component-name',{options}) 全局注册子组件
let myExennd = Vue.extend({ data(){ return { name:'zeng' } }, template:`<span> to {{name}} </span>`, }) Vue.component('my-component',myExennd) let vm = new Vue({ el:'#vm' })
|
结论
Vue.component()接收第二参数的时候,会判断是构造器还是{options}选项,如果是后者,就会隐式调用Vue.extend()返回构造器