Vue学习(动态组件、组件路由缓存keepalive)-学习笔记

Vue学习(动态组件、组件路由缓存keepalive)-学习笔记

动态组件

<template>
<div>
  <button @click="flag='testA'">testA</button>
  <button @click="flag='testB'">testB</button>
  <button @click="flag='testC'">testC</button>
  <!-- <testA  v-if="flag=='testA'"/>
  <testB v-if="flag=='testB'"/> -->
  <!-- 动态组件  flag的值与组件的名称相匹配-->
  <component :is="flag"></component>
</div>
</template>

<script>
import testA from './testA.vue'
import testB from './testB.vue'

export default {
  name:'',
  data(){
    return{
      flag:'testA'
    }
  },
  components:{
    testA,
    testB
  }
}
</script>

组件路由缓存keepalive

<template>
<div>
  <button @click="flag='testA'" :class="flag=='testA'?'active':''">testA</button>
  <button @click="flag='testB'"  :class="flag=='testB'?'active':''">testB</button>
  <button @click="flag='testC'"  :class="flag=='testC'?'active':''">testC</button>
  <!-- <testA  v-if="flag=='testA'"/>
  <testB v-if="flag=='testB'"/> -->
  <!-- 动态组件  flag的值与组件的名称相匹配-->
  <!-- keep-alive  抽象组件  用于对组件进行缓存 -->
  <!-- include:只有匹配的组件才会缓存 (可写正则表达式) -->
  <!-- exclude:任何组件不会缓存 -->
  <keep-alive include="testA">
     <component :is="flag"></component>
  </keep-alive>

  <!-- 组件testA、testC缓存 -->
  <!-- <keep-alive include="testA,testC">
     <component :is="flag"></component>
  </keep-alive>-->

  <!-- 除了组件testA、testC缓存 -->
  <!-- <keep-alive exclude="testA,testC">
     <component :is="flag"></component>
  </keep-alive>-->
</div> 
  
</template>

<script>
import testA from './testA.vue'
import testB from './testB.vue'

export default {
  name:'',
  data(){
    return{
      flag:'testA'
    }
  },
  components:{
    testA,
    testB
  }
}
</script>

<style scoped>
@import '../../assets/css/table.css';
.active {
  background:#f00;
}
</style>
<template>
<div>
  testA组件:{{title}}
</div>
</template>

<script>
export default {
  name:'',
  data(){
    return{
      title:Math.random()
    }
  }
}
</script>
<template>
<div>
  testB组件:
  <input type="text" v-model="title" />
</div>
</template>

<script>
export default {
  name:'',
  data(){
    return{
      title:Math.random()
    }
  }
}
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值