为什么会错误vue-routeresmjs8c4f2100 Uncaught in promise NavigationDuplicated _name NavigationDuplicated name NavigationDuplicated message Navigating to current location csa=244 is not allowed stack Errorn
这个错误通常是由于在Vue应用程序中重复导航到当前位置引起的。这可能是在代码中多次触发相同的路由导航,或者在路由跳转之前未正确处理异步操作导致的。解决此问题的一种方法是在进行路由导航之前检查当前路由是否与目标路由相同,并在导航重复时阻止路由跳转。可以使用以下代码来实现此功能:
import { NavigationDuplicated } from 'vue-router';
router.beforeEach((to, from, next) => {
if (to.path === from.path) {
next(false); // 取消路由跳转
throw new NavigationDuplicated(); // 抛出错误
} else {
next(); // 继续路由跳转
}
});
这将在路由导航重复时抛出 NavigationDuplicated 错误,以防止重复导航到当前位置。同时,也可以使用 next(false) 来取消路由跳转。
原文地址: https://www.cveoy.top/t/topic/6xi 著作权归作者所有。请勿转载和采集!