每个组件里展示的是一个tab里的内容,先注册3个组件:
// tab0
Vue.component('item0',{
template : '<div>1111111content</div>'
});
// tab1
Vue.component('item1',{
template : '<div>222222content</div>'
})
// tab2
Vue.component('item2',{
data(){
return{
message : ''
}
},
template : `<div>
<span style="color:#f00">hello world</span>
<p><input type="text" v-model="message"></p>
<p>{{message}}</p>
</div>`
})然后在html中使用component来展示对应组件的内容,title的展示方式不变:
<div class="hd">
<ul class="clearfix">
<li v-for="(item, index) of list" :class="{active:selected==index}" @mouseenter="change(index)">{{item.title}}</li>
</ul>
</div>
<component :is="currentView"></component>currentView属性可以让多个组件可以使用同一个挂载点,并动态切换:
var app = new Vue({
el: '#app',
data: {
selected: 0,
currentView : 'item0',
list: [
{
title: '11111'
},
{
title: '22222'
},
{
title: '33333'
}
] },
methods: {
change(index) {
this.selected = index;
this.currentView = 'item'+index; // 切换currentView
}
}
})这样 message 在组件里就是一个独立的data属性,能在tab里也使用vue绑定事件了.
3. 使用slot方式等
使用
slot方式进行内容分发或者一个独立的组件,可以让我们把代码整合到一块,对外提供一个数据接口,只要按照既定的格式填写数据即可。3.1 slot
用slot方式写一个子组件:
Vue.component('my-slot-tab', {
props : ['list', 'selected'],
template : `<div class="tab">
<div class="hd">
<ul class="clearfix">
<slot name="title" v-for="(item, index) in list" :index="index" :text="item.title"> </slot>
</ul>
</div>
<div class="bd">
<slot name="content" :content="list[selected].content"></slot>
</div>
</div>`
});父组件模板:
<my-slot-tab :list="list" :selected="selected">
<template slot="title" scope="props">
<li :class="{active:selected==props.index, item:true}" @mouseenter="change(props.index)">{{ props.text }}</li>










