. 避免初始化逻辑重复执行,如数据加载
. 不是简单的使用缓存组件包裹,而应该使用动态组件实现;注意查看浏览器的提示信息
. v-slot="{ Component }" 是 Vue 3 中用于接收
作用域插槽 scoped slot 传递数据的语法:从
<RouterView> 接收当前激活的路由组件
<RouterView v-slot="{ Component }">
<KeepAlive>
<component :is="Component" />
</KeepAlive>
</RouterView>
其它方案:使用前先判断
父组件
<div class="father">
<MallPinia></MallPinia>
</div>
import MallPinia from '@/components/MallPinia.vue';
子组件
onMounted(async () => {
if (!goodsStore.goods.length) {
console.log('first');
goods.value = await goodsStore.getAll();
} else {
goods.value = goodsStore.goods;
}
})
如何缓存特定路由?