vue3 keep-alive 如何实现 相同组件 查询参数不同实现不同实例缓存?

新手上路,请多包涵

vue3 keep-alive 如何实现 相同组件 查询参数不同实现不同实例缓存?

例子1:
页面1:/article/123
页面2:/article/234

例子2:
页面1:/article/detail?id=132
页面2:/article/detail?id=324

实现两个不同的实例,修改页面数据时互不干扰。

页面结构

<template>
  <router-view v-slot="{ Component, route }">
    <keep-alive :include="cachedViews">
      <component
        v-if="route.meta.keepAlive && cachedViews.includes(route.name)"
        :is="Component"
      />
    </keep-alive>
    <component
      v-if="!route.meta.keepAlive || !cachedViews.includes(route.name)"
      :is="Component"
    />
  </router-view>
</template>
<script>
import store from "@/store";
import { computed, watch, ref, onMounted } from "@vue/runtime-core";
export default {
  setup() {
    const cachedViews = computed(() => {
      return store.state.fixedTab.cachedViews;
    });

    return {
      cachedViews,
    };
  },
};
</script>
阅读 2.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题