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 保持活动缓存路由
基础教程推荐
- Bokeh Div文本对齐 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
