动态组件

动态组件

动态组件:动态切换组件的显示与隐藏

通过两个按钮,进行Left和Right两个组件的切换

通过keep-alive include和exclude进行组件的选择缓存还是不缓存

而keep-alive有对应的生命周期activated、deactivated

步骤

普通的导入后,再加入

组件生命周期

Left.vue

  created(){
    console.log('Left被创建啦!');
  },
  destroyed() {
    console.log('Left被销毁啦!');
  },
  // keep-alive对应生命周期
  activated(){
    console.log('Left组件被激活啦!');
  },
  deactivated(){
    console.log('Left组件被缓存啦!');
  }

App.vue

  <div id="app">
    <button @click="comName = 'Left'">展示Left</button>
    <button @click="comName = 'Right'">展示Right</button>
    <keep-alive include="Left,Right" >
      <component :is="comName"></component>
    </keep-alive>
  </div>
<!-- keep-alive include 指定哪些组件会把内部的组件进行缓存,而exclude销毁组件 -->
  data() {
    return {
      comName:Left
    }
  }
posted @   Dinesaw  阅读(58)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2022-03-28 2.4线性反馈移位寄存器
2022-03-28 博客背景
2022-03-28 2.3二元序列的伪随机性
2022-03-28 2.2 如何生成密钥流
2022-03-28 2.1 流密码
点击右上角即可分享
微信分享提示