首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue3获取保持活动缓存的组件

Vue3获取保持活动缓存的组件
EN

Stack Overflow用户
提问于 2021-10-09 06:41:54
回答 1查看 75关注 0票数 0

我使用vuejs3+typescript构建了一个后台管理系统,并使用keep-alive来缓存页面。如何获取当前缓存的组件?

EN

回答 1

Stack Overflow用户

发布于 2021-10-10 02:29:05

<keep-alive>组件exposes its cache as __v_cache。您可以在<keep-alive>上使用template ref,并从其内部实例($)读取__v_cache

代码语言:javascript
复制
<template>
  <button @click="logKeepAliveCache">Log keep-alive cache</button>

  <router-view v-slot="{ Component }">
    <keep-alive ref="keepAlive">
      <component :is="Component" />
    </keep-alive>
  </router-view>
</template>

<script setup>
import { ref } from 'vue'

const keepAlive = ref()

const logKeepAliveCache = () => {
  // The cache is a `Map`, where the entry keys are
  // metadata about the component's file, and the
  // filename is stored in `__file`.
  console.log([...keepAlive.value.$?.__v_cache.keys()].map((x) => x.__file))
}
</script>

注意:__v_cache在生产版本中不可用。

demo

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69504354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档