13.vue动态组件
2018-07-07 本文已影响19人
yaoyao妖妖
1.vue动态组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>组件的插槽(slot)</title>
<!--引入/vue.js-->
<script src="./vue.js"> </script>
</head>
<body>
<div id="root">
<child-one v-if="type === 'child-one'">child-one</child-one>
<child-two v-if="type === 'child-two'">child-two</child-two>
<button @click="handleBtnClick">change</button>
</div>
<script>
Vue.component('child-one',{
template:'<div>child-one</div> '
})
Vue.component('child-two',{
template:'<div>child-two</div>'
})
var app = new Vue({
el:'#root',
data:{
type:'child-one'
},
methods:{
handleBtnClick:function(){
this.type = (this.type === 'child-one') ? 'child-two' : 'child-one'
}
}
})
</script>
</body>
</html>
等同于上一个例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>组件的插槽(slot)</title>
<!--引入/vue.js-->
<script src="./vue.js"> </script>
</head>
<body>
<div id="root">
<!-- component动态组件 -->
<component :is="type">child-one</component>
<button @click="handleBtnClick">change</button>
</div>
<script>
Vue.component('child-one',{
template:'<div v-once>child-one</div> '
})
Vue.component('child-two',{
template:'<di v-once>child-two</di>'
})
var app = new Vue({
el:'#root',
data:{
type:'child-one'
},
methods:{
handleBtnClick:function(){
this.type = (this.type === 'child-one') ? 'child-two' : 'child-one'
}
}
})
</script>
</body>
</html>