動態(tài)組件與v-once指令

使用v-once指令提高靜態(tài)內(nèi)容展示效率,減少頻繁創(chuàng)建和銷毀dom的操作;

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

<head>
  <meta charset="utf-8">
  <title>動態(tài)組件與v-once指令</title>
  <script src='./vue.js'></script>
</head>

<body>
  <div id="app">
    <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>
    // 不使用動態(tài)組件
    Vue.component('child-one', {
      template: '<div>child-one</div>'
    })
    Vue.component('child-two', {
      template: '<div>child-two</div>'
    })
    var vm = new Vue({
      el: '#app',
      data: {
        type: 'child-one'
      },
      methods: {
        handleBtnClick: function() {
          this.type = this.type === 'child-one' ? 'child-two' : 'child-one'
        }
      }
    })
  </script>
</body>

</html>
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>動態(tài)組件與v-once指令</title>
  <script src='./vue.js'></script>
</head>

<body>
  <div id="app">
    <component :is="type"></component>
    <button @click="handleBtnClick">change</button>
  </div>

  <script>
    // 使用動態(tài)組件
    Vue.component('child-one', {
      template: '<div>child-one</div>'
    })
    Vue.component('child-two', {
      template: '<div>child-two</div>'
    })
    var vm = new Vue({
      el: '#app',
      data: {
        type: 'child-one'
      },
      methods: {
        handleBtnClick: function() {
          this.type = this.type === 'child-one' ? 'child-two' : 'child-one'
        }
      }
    })
  </script>
</body>

</html>
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>動態(tài)組件與v-once指令</title>
  <script src='./vue.js'></script>
</head>

<body>
  <div id="app">
    <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>
    // 使用v-once指令提高靜態(tài)內(nèi)容展示效率,減少頻繁創(chuàng)建和銷毀dom的操作
    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: '#app',
      data: {
        type: 'child-one'
      },
      methods: {
        handleBtnClick: function() {
          this.type = this.type === 'child-one' ? 'child-two' : 'child-one'
        }
      }
    })
  </script>
</body>

</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容