Vue 动态组件的理解

什么是动态组件
让多个组件使用同一个挂载点,并动态切换,这就是动态组件
<template>
  <div>
     <div>动态渲染组件</div>
     <div>
     //component标签就是挂载点,  :is=绑定的组件名字
       <component :is="currentComponent"></component>
     </div>
     <button @click="btn_click()">按钮</button>
  </div>
</template>

<script>
import Tab0 from "@/components/Tab0.vue";
import Tab1 from "@/components/Tab1.vue";
export default {
  name:'About',
  data(){
    return{
      currentIndex:0
    }
  },
  components:{
    Tab0,
    Tab1
  },
  methods:{
    btn_click(){
      if(this.currentIndex==0){
        this.currentIndex=1
      }else{
        this.currentIndex = 0
      }
    }
  },
  computed:{
    currentComponent:function(){
      return `Tab${this.currentIndex}`
    }
  }
}
</script>

每次切换都是生成新的组件,如果用keep-alive包裹,就可以提高性能,前提是大项目!相对提升性能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值