Vue 动态组件缓存(vue-cli)

App.vue

<template>
  <div id="app">
    <!-- keep-alive保证这种需要输入的标签中的内容缓存不被销毁 -->
    <keep-alive>
      <!-- 这里的componentxx名字随便取 -->
      <component :is="componentxx"></component>
    </keep-alive>
    <br />
    <button @click="componentxx='A'">change to A</button>
    <button @click="componentxx='B'">change to B</button>
  </div>
</template>

<script>
import A from "./components/A";
import B from "./components/B";

export default {
  name: "app",
  components: {
    A,
    B
  },
  data() {
    return {
      componentxx: "A"
    };
  }
};
</script>

component-->A.vue

<template>
  <div class="a">
    <h2>A component</h2>
    <input type="text" />
  </div>
</template>

component-->B.vue

<template>
  <div class="b">
    <h2>B component</h2>
    <input type="text" />
  </div>
</template>

效果图:


a.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容