iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue-router4版本第一次打开界面不匹配路由问题解决
  • 839
分享到

vue-router4版本第一次打开界面不匹配路由问题解决

vue-router4界面不匹配路由vue-router4路由匹配 2023-05-15 17:05:51 839人浏览 独家记忆
摘要

问题:[Vue Router warn]: No match found for location with path “/home” 因为以前是一次性添加路

问题:[Vue Router warn]: No match found for location with path “/home”

因为以前是一次性添加路由使用的是addRoutes,现在成了addRoute一个一个添加,我登陆后动态添加路由后,明明已经加了路由,打开却警告不匹配而且一片空白,然后查了说是需要

next({...to,replace:true})

这个…to,表示会再去一次这个路径,才能激活那个路径的匹配
以下是我的登录和加载菜单的逻辑和写法

login() {
  this.$refs.ruleFORM.validate(valid => {
    if (valid) {
      this.axiOS.postForm('/login',this.loginForm).then(response => {
        let data = response.data;
        this.$store.commit('login', data)
        let redirect = this.$route.query.redirect
        this.$router.push({path: (redirect === undefined) ? '/home' : redirect});
      })

    } else {
      return false;
    }
  });
}

登录后会跳转,然后触发全局守卫

router.beforeEach((to, from, next) => {//配置路由守卫
    if(to.path==='/'){
        next()
    }else if(store.state.user.id){
        initMenus(router,store,next,to)
    }else{
        next({ path: '/',query: {redirect: to.path}});
    }
});

然后第一次会进入initMenus函数初始化路由

import axios from "axios";
export const initMenus = (router, store,next,to) => {//按F5刷新的话vuex里的会被清空,长度变为0
    if (store.state.menu !== null) {
        next()
    }else {
        axios.get("/menu").then(response => {
            if (response) {
                let responseData = response.data
                if (responseData.flag) {
                    store.state.menu = responseData.data
                    initRoute(router,store.state)
                    next({...to,replace:true})//解决router4版本的第一次路由不匹配问题
                } else {
                    this.$ElMessage.error('请求菜单失败')
                }
            }
        })
    }
}

const initRoute = (router,state)=> {
    const loadView = view => {//这种引入方式控制台不会报警告
        // 路由懒加载
        return () => import(`@/views/${view}`)
    };
    const menus = state.menu
    const firstLevelMenu = {
        children: [],
        component: loadView('home/HomeView.vue')
    }
    menus.forEach(menu=>{
        menu.component = loadView(menu.component)
        if(menu.children === null || menu.children.length === 0){
            firstLevelMenu.children.push(menu)
        }else{
            menu.children.forEach(children=>{
                children.component = loadView(children.component)
            })
            router.addRoute(menu)
        }
    })
    router.addRoute(firstLevelMenu)
}

一定要在添加完所有路由后写这个next({…to,replace:true}),而且next不能重复调用

最后可以解决界面空白问题,但是警告不会消失

到此这篇关于vue-router4版本第一次打开界面不匹配路由问题解决的文章就介绍到这了,更多相关vue-router4界面不匹配路由内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: vue-router4版本第一次打开界面不匹配路由问题解决

本文链接: https://www.lsjlt.com/news/209341.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作