动态组件与V-once指令(4-8)

动态组件与V-once指令

使用v-once性能会更好。
使用的时候就在Vue实例里加入template模板为内容即可。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src='./vue.js'></script>
</head>
<body>
    <!-- 使用v-once那么性能会更好,下次加载只会加载一次 -->
    <div id="root" v-once>
        <!-- <component :is="type"></component> -->
        <!-- <child-one v-if="type === 'child-one'"></child-one>
        <child-two v-if="type === '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 vm = new Vue({
     
     
            el: '#root',
            data: {
     
     
                type: 'child-one'
            },
            template:`
              <div>
                <child-one v-if="type === 'child-one'" />
                <child-two v-if="type === 'child-two'" />
                <button @click="handleBtnClick">change</button>
              </div>  
            `,
            methods: {
     
     
                handleBtnClick: function() {
     
     
                    this.type = (this.type === 'child-one' ? 'child-two': 'child-one');
                }
            }
        })
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_45647118/article/details/114005115
今日推荐