动态组件与v-on

实现点击按钮切换内容
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
</head>
<body>
<div id="root">
<child-one v-if=" type ==='child-one'"></child-one>
<child-two v-if=" type ==='child-two'"></child-two>
<button @click="handleBtn">change</button>
</div>
<script>
Vue.component('child-one',{
template:'<div>child-one</div>'
})
Vue.component('child-two',{
template:'<div>child-two</div>'
})
var vm = new Vue({
el:"#root",
data:{
type:'child-one'
},
methods:{
handleBtn:function  () {
this.type=(this.type==='child-one'?'child-two':'child-one')
}
}

})
</script>

</body>
</html>




<template :is="type"></template>//template是动态组件,会根据is里面数据的动态变化,来自动加载不同的组件




//v-once可以提高性能,在切换的过程中,把不显示的内容放入缓存
Vue.component('child-one',{
template:'<div v-once>child-one</div>'
})
Vue.component('child-two',{
template:'<div v-once>child-two</div>'
})

猜你喜欢

转载自blog.csdn.net/qq_41153478/article/details/80365596