<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态组件</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<button v-for="(item,index) in tabs" v-on:click="templateStr=item">{{item}}</button>
<span>{{templateStr}}</span>
<component v-bind:is="templateStr"></component>
</div>
<script>
new Vue({
el:"#app",
data:{
tabs:["a1","b1","c"],
templateStr:'a1'
},
components:{
a1:{template:'<div>这是子组件1</div>'},
b1:{template:'<div>这是子组件2</div>'},
c:{template:'<div>这是子组件3</div>'}
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态组件</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<button v-for="(item,index) in tabs"
v-on:click="templateStr=item.value">{{item.name}}</button>
<component v-bind:is="templateStr"></component>
</div>
<script>
Vue.component('zhuYe',{
template:'<div>这是子组件--主页</div>'
});
Vue.component('xiangQing',{
template:'<div>这是子组件--详情</div>'
});
Vue.component('upFile',{
template:'<div>这是子组件--上传</div>'
});
new Vue({
el:"#app",
data:{
tabs:[{value:'zhuYe',name:'主页'},{value:'xiangQing',name:'详情'},
{value:'upFile',name:'上传'}],
templateStr:'zhuYe'
}
});
</script>
</body>
</html>