Componentes dinámicos e instrucciones V-once (4-8)

Componentes dinámicos e instrucciones V-once

Use v-once, el rendimiento será mejor.
Cuando lo use, simplemente agregue la plantilla de plantilla como contenido a la instancia de Vue.

<!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>

Supongo que te gusta

Origin blog.csdn.net/weixin_45647118/article/details/114005115
Recomendado
Clasificación