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

墨蓝 2022-10-04 15:00 296阅读 0赞

文章目录

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

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

动态组件

  1. <template>
  2. <div>
  3. <button @click="flag='testA'">testA</button>
  4. <button @click="flag='testB'">testB</button>
  5. <button @click="flag='testC'">testC</button>
  6. <!-- <testA v-if="flag=='testA'"/> <testB v-if="flag=='testB'"/> -->
  7. <!-- 动态组件 flag的值与组件的名称相匹配-->
  8. <component :is="flag"></component>
  9. </div>
  10. </template>
  11. <script> import testA from './testA.vue' import testB from './testB.vue' export default { name:'', data(){ return{ flag:'testA' } }, components:{ testA, testB } } </script>

组件路由缓存keepalive

  1. <template>
  2. <div>
  3. <button @click="flag='testA'" :class="flag=='testA'?'active':''">testA</button>
  4. <button @click="flag='testB'" :class="flag=='testB'?'active':''">testB</button>
  5. <button @click="flag='testC'" :class="flag=='testC'?'active':''">testC</button>
  6. <!-- <testA v-if="flag=='testA'"/> <testB v-if="flag=='testB'"/> -->
  7. <!-- 动态组件 flag的值与组件的名称相匹配-->
  8. <!-- keep-alive 抽象组件 用于对组件进行缓存 -->
  9. <!-- include:只有匹配的组件才会缓存 (可写正则表达式) -->
  10. <!-- exclude:任何组件不会缓存 -->
  11. <keep-alive include="testA">
  12. <component :is="flag"></component>
  13. </keep-alive>
  14. <!-- 组件testA、testC缓存 -->
  15. <!-- <keep-alive include="testA,testC"> <component :is="flag"></component> </keep-alive>-->
  16. <!-- 除了组件testA、testC缓存 -->
  17. <!-- <keep-alive exclude="testA,testC"> <component :is="flag"></component> </keep-alive>-->
  18. </div>
  19. </template>
  20. <script> import testA from './testA.vue' import testB from './testB.vue' export default { name:'', data(){ return{ flag:'testA' } }, components:{ testA, testB } } </script>
  21. <style scoped> @import '../../assets/css/table.css'; .active { background:#f00; } </style>
  22. <template>
  23. <div>
  24. testA组件:{
  25. {title}}
  26. </div>
  27. </template>
  28. <script> export default { name:'', data(){ return{ title:Math.random() } } } </script>
  29. <template>
  30. <div>
  31. testB组件:
  32. <input type="text" v-model="title" />
  33. </div>
  34. </template>
  35. <script> export default { name:'', data(){ return{ title:Math.random() } } } </script>

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读