vuejs3 - vue3, vue-router4.0 instances.default 在路由匹配中未定义
问题描述
我正在使用vue3,vue-router4进行一个项目,并且在最新的vue和vue-router中遇到了一些问题。在 vue2 和 vue-router3.0 中,当我在处理嵌套路由器时,我做了一个HOC
可以渲染component
or的router-view
,但是在 vue3 和 vue-router4.0 中,我无法instances.default
通过route.matched
。
Vue2、Vue-Router3
export default function routeReplaceSelf(component) {
return {
name: "routeReplaceSelf",
computed: {
showChild() {
const deepestMatchedRoute = this.$route.matched[this.$route.matched.length - 1];
return deepestMatchedRoute.instances.default !== this;
},
},
render(h) {
const child = this.showChild ? h("router-view") : h(component);
return h("keep-alive", [child]);
},
};
}
Vue3、Vue-Router4
import { h, computed, getCurrentInstance } from "vue";
import { useRoute, useRouter, onBeforeRouteLeave } from "vue-router";
export default function routeReplaceSelf(component) {
return {
name: "routeReplaceSelf",
setup(props) {
const instance = getCurrentInstance();
const route = useRoute();
const router = useRouter();
const showChild = computed(() => {
const deepestMatchedRoute = route.matched[route.matched.length - 1].instances.default;
return deepestMatchedRoute !== instance;
});
const child = showChild.value ? h("router-view") : h(component);
return () => h("keep-alive", [child]);
},
};
}
结果总是得不到router-view
,component
原因instances.default
总是undefined
。
希望有人可以帮助我,谢谢。
解决方案
推荐阅读
- php - 插入多个数据和多个图像 Laravel
- shopify - 如何在 shopify 产品页面上添加自定义文本框字段
- sql-server - 关键字“with”附近的语法不正确。SQL Server 速成版
- amazon-dynamodb - Dynamodb GSI 查询响应很慢
- python - 带有 inline_requests 装饰器的单元测试 Scrapy 解析函数
- python - 当前不支持 BoringSSL 的 ASM 构建:linux-aarch64
- android - Android GUI - 如何将通用样式应用于多个元素
- postgresql - connecting springboot application running locally to db instance running on aws VM instance
- python - Python xml 解析器如何检测编码(utf-8 vs utf-16)?
- javascript - javascript中console.log和alert的区别