Vue.js 组件的2种创建使用方法

laipan27 / 2023-09-04 / 原文

创建组件的两种方式

1.全部注册组件
Vue.commpent('自定义标签名',{
//组件有自己的一个data函数,也只能是函数
组件不可有根节点el属性
data:function(){

return {
//返回data中的数据
}

//模板
template:`

`,

methods:{
//组件的方法
},

computed:{
//计算属性
},

watch: {
//侦听器

}
})

2.局部注册

Vue.extend({

data:function(){

return {
//返回data中的数据
}

//模板
template:`

`,

methods:{
//组件的方法
},

computed:{
//计算属性
},

watch: {
//侦听器

}


})
//局部组件需要在Vue实例中注册该组件,而全局组件则不需要
new Vue({
el'',
data:{
},
components:{
//注册组件

组件名(自定义标签名):组件实例所值的变量值
todolist:zujian1

}

})