vue_19 v-once :is

动态组件和v-once命令

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>slot</title>
    <script src="./vue.js"></script>
</head>
<!-- v-once加载到内存,重复使用 -->
<body>

<div id="root">
<component :is="type"></component>
    <!-- <child-one v-if="type ==='child-one'"></child-one>
    <child-two v-if="type ==='child-two'"></child-two> -->
    <button @click="handleClick">change</button>
</div>

<script>
Vue.component('child-one',{
    template:'<div v-once>child-one</div>'
})
Vue.component('child-two',{
    template:'<div v-once>child-two</div>'
})

var vm = new Vue({
    el:"#root",
    data:{
        type:'child-one'
    },
    methods:{
        handleClick:function(){
            this.type = (this.type === 'child-one' ? 'child-two':'child-one')
        }
    }
})
</script>

</body>

</html>
vue 组件动态绑定和v-once

推荐阅读更多精彩内容