欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

is attribute 来切换不同的组件

程序员文章站 2022-05-15 13:45:16
...
  <div id="demo">
    <component v-bind:is="currentComponent">taozi</component>
    <button @click="click">切换</button>
  </div>
  
  <script src="vue.js" type="text/javascript" charset="utf-8"></script>
  <script>
    Vue.component('slot-welcome',{
      template:`<div>
        <p>welcome</p>
        <slot></slot>
      </div>`
    })
    Vue.component('slot-hello',{
      template:`<div>
        <p>hello</p>
        <slot></slot>
      </div>`
    })
   var vm = new Vue({
      el:"#demo",
      data:{
        currentComponent:'slot-hello'
      },
      methods:{
        click(){
          if(this.currentComponent === "slot-welcome"){
            this.currentComponent = "slot-hello"
          }else {
            this.currentComponent = "slot-welcome"
          }
        }
      }
    })
    //参考: https://kaven.blog.csdn.net/article/details/109621293
  </script>

 

相关标签: vue教程