Vue 3 keep-alive cache routes(Vue 3 保持活动缓存路由)
问题描述
我正在使用 Vue 3 <keep-alive>.当我不使用 :key 时,它会缓存(按预期不正确地跨不同的 URL).
I'm using Vue 3 <keep-alive>. When I don't use :key it caches (improperly across different URLs as expected).
通过添加
<router-view :key="$route.fullPath" v-slot="{ Component }">
<keep-alive>
<component :is="Component" />
</keep-alive>
</router-view>
我认为它只会在缓存键不存在的情况下进行 API 调用,这样当我转到不同的路线并返回时,它不会进行第二次 API 调用.
I would think that it only makes an API call if the cache key doesnt exist, so that when I go to a different route and come back, it won't make a second api call.
但是当我现在添加 :key="$route.fullPath" 时,即使我重新访问同一个 URL,它每次都会调用 API?
But when I add :key="$route.fullPath" now it makes an API call every single time even if I revisit the same URL?
推荐答案
在 Vue 3 中,将 key 放在 而不是 <路由器视图>:
In Vue 3, put the key on the <component> rather than the <router-view>:
<router-view v-slot="{ Component }">
<keep-alive>
<component :is="Component" :key="$route.fullPath"></component>
</keep-alive>
</router-view>
这篇关于Vue 3 保持活动缓存路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Vue 3 保持活动缓存路由
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
