代码>,但它们似乎没有被缓存.当我在 products 之间切换时,每个 product 仍会触发 mounted()事件.Inside of products are individual product components which get mounted when the route is opened. My problem is that when I switch between the routes, and the filter parameter is changed, the product components get remounted every time. I'd like to cache them so switching back and forth is easier. To do this I set up <keep-alive> and added :key='$route.fullPath' to my <router-view> but they don't seem to be cached. Each product is still firing a mounted() event when i switch between products.<keep-alive> <router-view :key='$route.fullPath'></router-view></keep-alive>我应该将每个 products 视图放入一个单独的组件中吗?Should I make each products view into a separate component?推荐答案要在路由器视图中使用keep-alive,应在视图路由器上使用唯一键,如下所示:For use keep-alive with router view you should use unique key on view router like this: <keep-alive> <router-view :key="$route.fullPath"></router-view> </keep-alive>不要忘记在保持活动状态时使用max属性来防止内存开销Dont forgot use max attribute on keep alive to prevent memory overhead<keep-alive max="5">或仅包含要缓存的组件:or just include components want cache:<keep-alive include="FirstComp,SecondComp"> 对于组件中的每个组件,如果要缓存,则需要保持活动状态And for each components inside component you need keep alive them if you want cache<keep-alive> <product></product></keep-alive> 这篇关于Vue 2&lt; keep-alive&gt;不适用于&lt; router-view&gt;和钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-13 12:27