3个页面homedirectoryanimal其中directory使用了keepalive这样就可以实现从directory跳转到animal再返回directory时directory中的标签回到对应的位置但是从home进入到directory时还是到了缓存中的位置怎么设置才能让从home进入到directory时不用directory中keepalive中的值而重新初始化呢用vue3加set
在使用Vue 3的setup语法糖时,可以使用computed属性来判断当前路由是否为从home进入directory,如果是,则将keepalive中的值置为false,重新初始化directory页面。
具体代码如下:
import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
export default {
name: 'Directory',
setup() {
const route = useRoute()
const keepAlive = ref(true)
const fromHome = computed(() => {
return route.from.name === 'Home'
})
if (fromHome.value) {
keepAlive.value = false
// 重新初始化directory页面的代码
}
return {
keepAlive
}
}
}
在上述代码中,我们使用useRoute获取当前路由信息,然后使用computed属性判断是否为从home进入directory。如果是,则将keepalive的值置为false,并且可以在if语句中加入重新初始化directory页面的代码,以达到重新初始化的目的。
最后,我们将keepAlive暴露出去,以便在模板中使用。
原文地址: https://www.cveoy.top/t/topic/bPL2 著作权归作者所有。请勿转载和采集!