美文网首页
动态组件与v-once指令

动态组件与v-once指令

作者: 秋玄语道 | 来源:发表于2018-06-29 23:41 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态组件</title>
    <script src="js/vue.js"></script>
    <link rel="stylesheet" href="css/demo.css">
</head>
<body>
<div id="app">
    <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:'#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">
<head>
    <meta charset="UTF-8">
    <title>v-once指令</title>
    <script src="js/vue.js"></script>
    <link rel="stylesheet" href="css/demo.css">
</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>
  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>

相关文章

网友评论

      本文标题:动态组件与v-once指令

      本文链接:https://www.haomeiwen.com/subject/wtvzyftx.html