点击切换child-one和child-two显示与隐藏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态组件与v-once指令</title>
<script src="../js/vue.js" type="text/javascript"></script>
</head>
<body>
<div id="app">
<child-one :content="result" v-if="result == 'childOne'"></child-one>
<child-two :content="result" v-if="result == 'childTwo'"></child-two>
<button @click="change">变化</button>
</div>
</body>
<script>
Vue.component('child-one',{
props:['content'],
template:'<div>{{content}}</div>'
})
Vue.component('child-two',{
props:['content'],
template:'<div>{{content}}</div>'
})
var app = new Vue({
el:"#app",
data:{
result:'childOne'
},
methods:{
change:function () {
this.result = this.result === 'childOne' ? 'childTwo' : 'childOne';
}
}
})
</script>
</html>