本文最后更新于 868 天前,其中的信息可能已经有所发展或是发生改变。
var login = Vue.extend({
template: '<h1>登录</h1>'
});
Vue.component('login', login);
Vue.component('register', {
template: '<h1>注册</h1>'
});
<script id="tmpl" type="x-template">
<div><a href="#">登录</a> | <a href="#">注册</a></div>
</script>
同时,需要使用 Vue.component 来定义组件:
Vue.component('account', {
template: '#tmpl'
});
var tmp1={
template:'<h1> helle Vue 组件</h1>
}
Vue.component('myCom5',tmp1)
var tmp2={
template:'<h1> helle Vue 组件6</h1>'
}
var vm = new Vue({
el:"#app",
data:{
},
methods:{
},
components:{
tmp2
}
})
<tmp2></tmp2>
注意: 组件中的DOM结构,有且只能有唯一的根元素(Root Element)来进行包裹! 如果使用 Vue.component 定义全局组件的时候,组件名称使用了 驼峰命名,则在引用组件的时候,需要把 大写的驼峰改为小写的字母,同时,两个单词之前,使用 - 链接; 如果不使用驼峰,则直接拿名称来使用即可;
Vue.component('mycom1', {
template: '<h1>这是全局组件 --- {{msg}}</h1>',
data: function () {
return {
msg: '这是组件的中data定义的数据'
}
}
})
Vue.component('counter', {
template: '#tmpl',
data: function () {
// return dataObj
return { count: 0 }
},
methods: {
increment() {
this.count++
}
}
})
Vue提供了 component ,来展示对应名称的组件 component 是一个占位符, :is 属性,可以用来指定要展示的组件的名称
<component :is="comName"></component>
总结:当前学习了几个 Vue 提供的标签了 component, template, transition, transitionGroup
this.$refs
来获取元素和组件<com1 ref="mytemplate"></com1>
<h3 ref="myh3">获取的元素</h3>
console.log(this.$refs.myh3.innerText)
this.$refs.mytemplate.tmp1show()
注意:关于组件的属性绑定都应该在使用组件的地方编写,而不是在组件的模板里编写
Post Views: 320