[Vue Router warn]: No match found for location with path “xxxxx“

这篇具有很好参考价值的文章主要介绍了[Vue Router warn]: No match found for location with path “xxxxx“。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在vue项目中,大家做权限管理的时候,大部分是采用addRoute方案来实现。

在之前使用vue-router的时候,大家在动态追加完路由后,还要再追加一下404页面,如果在路由文件中直接写好404页面,那么刷新页面的时候就会跳转到404页面,原因在于,我们在加动态路由前,就配置了通配符404路由.改成动态添加过路由后,再最后push一下404通配符,这样就可以了。

路由全局守卫:

router.beforeEach(async (to, from, next) => {
    ...// 其他逻辑省略,只看addRoutes部分
    try {
        await store.dispatch('GetUserInfo')
        const res = await store.dispatch('getAsyncRoutes')
        const asyncRoutes = handleRoutes(res)
        router.addRoutes(asyncRoutes);
        router.addRoutes({
            path: "*", 
            name: "404", 
            component: () => import("@/views/error/404") 
        })
        next({ ...to, replace: true })
    } catch (error) {
        next(`/login?redirect=${to.path}`)
    }
})

后来vue-router升级,如果还像上面那样动态追加404的话,会有问题:刷新页面,如果当前是动态追加的路由,控制台会报警告:

[Vue Router warn]: No match found for location with path “xxxxx”

这是因为,我们刷新页面或者第一次访问动态的路由,全局守卫beforeEach执行,但是这时候我们还没有动态追加路由,找不到,而且我们是后续追加的404,从而导致第一次路由的matched为空,所以就报了这个警告,请看刷新页面后,控制台打印的结果:

no match found for location with path,vue.js,javascript,前端

解决办法:

升级后的vue-router已经不需要再动态追加404路由了,直接写到初始化的router文件里就好:

router/index.ts:

import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
const routes: Array<RouteRecordRaw> = [
    ...// 其他路由省略
    {
        path: '/:catchAll(.*)',
        name: '404',
        component: () => import('@/views/error/index.vue')
    }
]
const router = createRouter({
    history: createWebHistory(),
    routes
})
export default router

全局守卫:

router.beforeEach(async (to) => {
    ...// 其他逻辑省略
    try {
        await userStore.getUserInfo()
        const res: any = await appStore.getAsyncRoutes()
        const asyncRoutes = handleRoutes(res)
        asyncRoutes.forEach((item) => router.addRoute('layout', item))
        return to.fullPath
    } catch (error) {
        return `/login?redirect=${to.fullPath}`
    }
})

这样,当刷新页面,第一次走beforeEach的时候,因为还没动态追加路由,会匹配到404,这样matched就不是空的,就不会报警告了:

no match found for location with path,vue.js,javascript,前端文章来源地址https://www.toymoban.com/news/detail-529550.html

到了这里,关于[Vue Router warn]: No match found for location with path “xxxxx“的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ERROR: No matching distribution found for gradio>=3.23

    今天运行chatGPTweb项目的时候 跟下载其他包时候一样使用清华源下载的时候,pip install gradio==3.23 -i https://pypi.python.org/pypi 然后, 报错了 。 国内的镜像源还没有更新到 gradio=3.23,所以需要科学上网,手动去pypi官网下载whl,然后通过whl下载即可。 1.从官网中下载gradio编译的pyd文

    2024年02月11日
    浏览(40)
  • No matching distribution found for torch==1.10.1+cu111

    30系显卡暂时不支持CUDA11以下版本,CUDA不支持当前显卡的算力。 解决方法1:https://blog.csdn.net/weixin_43760844/article/details/115706289 解决方法2:conda下载cudatoolkit (貌似没有解决问题, 嘿嘿, 可能只能卸载cuda了) 首先搜索安装包的版本 然后安装固定版本的cudatoolkit,我的cuda最高

    2024年02月07日
    浏览(32)
  • 已解决ERROR: No matching distribution found for gradio==3.23

    已解决stderr: ERROR: Could not find a version that satisfies the requirement gradio==3.23 ERROR: No matching distribution found for gradio==3.23 粉丝群里面的一个小伙伴遇到问题跑来私信我,想用pip安装gradio ,但是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记

    2023年04月18日
    浏览(32)
  • jenkins构建时,报错ERROR: No matching distribution found for pywin32==305

    最近用jenkin构建了一个任务,控制台输出,出现如下报错信息: ERROR: Could not find a version that satisfies the requirement pywin32==305 (from versions: none) ERROR: No matching distribution found for pywin32==305 Build step \\\'Execute shell\\\' marked build as failure Finished: FAILURE   原因: requirement是需要导入的依赖包文件

    2023年04月27日
    浏览(32)
  • No matching pid found... husky - pre-commit hook exited with code 1 (error)

    出现这个问题的主要原因是.husky下面的pre-commit已经存在了 删除掉这个pre-commit文件 再从stash里把内容Apply拿出来 提交就可以了  使用lint-staged规范提交的插件 有的时候也会出现 正在提交的时候 一直loading加载 或者提交内容突然没了 这种情况 也是在stash里面找到对应代码 继续

    2024年02月15日
    浏览(37)
  • linux ssh报错Unable to negotiate with **** port 22: no matching host key type found

    ssh连接机器报错 今天使用mac通过ssh连接机器的时候报错如上,查阅网上资料后解决,方法如下 保存后,测试可以ssh登录了,特此记录下解决方法

    2024年02月11日
    浏览(39)
  • npm ERR! notarget No matching version found for @eslint/eslintrc@^2.1.4.

    事由是今天我在用 create-react-app 新建一个用于测试的前端项目。 然后就出现以下报错: 截图见下(核心错误行已标红): 总结 首先,我怀疑了自己的 create-react-app node npm 的版本,怀疑了自己的网络问题,怀疑了我当前的操作目录问题。 这些都不是。 最终发现,是因为我自己

    2024年02月04日
    浏览(24)
  • Python安装tensorflow过程中出现“No matching distribution found for tensorflow”的解决办法

    在Pycharm中使用 pip install tensorflow 安装tensorflow时报错: 搜了好多帖子有的说可能是网络的问题,需要换国内的镜像源来下载,于是改用清华源: 依旧没用,折腾了好久,才发现我目前的Python版本是Python3.8(32位)的,可能是tensorflow对python3.8还不支持,所以得 降低python版本 (好

    2024年02月03日
    浏览(31)
  • python3安装及pip3 报ERROR: No matching distribution found for

    python3 pip Install Error: No matching distribution found for 安装openssl 安装python3 可能会报错: zipimport.ZipImportError: can’t decompress data 解决方法:

    2024年02月14日
    浏览(34)
  • 解决报错ERROR: No matching distribution found for torchvision==0.11.2+cu111

    目录 一、猜测 二、验证 三、解决方案 四、检验 该报错是在按官网方法用指令: 安装pytorch时出现的,以下是分析: 这个错误提示表明在指令提供的下载网址上没有找到符合要求的torchvision软件包版本,需要安装符合要求的版本。问题可能出在指定的版本号(0.11.2+cu111),这

    2024年02月11日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包