经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue-router4版本第一次打开界面不匹配路由问题解决
来源:jb51  时间:2023/4/14 9:17:49  对本文有异议

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

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

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

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

  1. login() {
  2. ? this.$refs.ruleForm.validate(valid => {
  3. ? ? if (valid) {
  4. ? ? ? this.axios.postForm('/login',this.loginForm).then(response => {
  5. ? ? ? ? let data = response.data;
  6. ? ? ? ? this.$store.commit('login', data)
  7. ? ? ? ? let redirect = this.$route.query.redirect
  8. ? ? ? ? this.$router.push({path: (redirect === undefined) ? '/home' : redirect});
  9. ? ? ? })
  10.  
  11. ? ? } else {
  12. ? ? ? return false;
  13. ? ? }
  14. ? });
  15. }

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

  1. router.beforeEach((to, from, next) => {//配置路由守卫
  2. if(to.path==='/'){
  3. next()
  4. }else if(store.state.user.id){
  5. initMenus(router,store,next,to)
  6. }else{
  7. next({ path: '/',query: {redirect: to.path}});
  8. }
  9. });

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

  1. import axios from "axios";
  2. export const initMenus = (router, store,next,to) => {//按F5刷新的话vuex里的会被清空,长度变为0
  3. ? ? if (store.state.menu !== null) {
  4. ? ? ? ? next()
  5. ? ? }else {
  6. ? ? ? ? axios.get("/menu").then(response => {
  7. ? ? ? ? ? ? if (response) {
  8. ? ? ? ? ? ? ? ? let responseData = response.data
  9. ? ? ? ? ? ? ? ? if (responseData.flag) {
  10. ? ? ? ? ? ? ? ? ? ? store.state.menu = responseData.data
  11. ? ? ? ? ? ? ? ? ? ? initRoute(router,store.state)
  12. ? ? ? ? ? ? ? ? ? ? next({...to,replace:true})//解决router4版本的第一次路由不匹配问题
  13. ? ? ? ? ? ? ? ? } else {
  14. ? ? ? ? ? ? ? ? ? ? this.$ElMessage.error('请求菜单失败')
  15. ? ? ? ? ? ? ? ? }
  16. ? ? ? ? ? ? }
  17. ? ? ? ? })
  18. ? ? }
  19. }
  20.  
  21. const initRoute = (router,state)=> {
  22. ? ? const loadView = view => {//这种引入方式控制台不会报警告
  23. ? ? ? ? // 路由懒加载
  24. ? ? ? ? return () => import(`@/views/${view}`)
  25. ? ? };
  26. ? ? const menus = state.menu
  27. ? ? const firstLevelMenu = {
  28. ? ? ? ? children: [],
  29. ? ? ? ? component: loadView('home/HomeView.vue')
  30. ? ? }
  31. ? ? menus.forEach(menu=>{
  32. ? ? ? ? menu.component = loadView(menu.component)
  33. ? ? ? ? if(menu.children === null || menu.children.length === 0){
  34. ? ? ? ? ? ? firstLevelMenu.children.push(menu)
  35. ? ? ? ? }else{
  36. ? ? ? ? ? ? menu.children.forEach(children=>{
  37. ? ? ? ? ? ? ? ? children.component = loadView(children.component)
  38. ? ? ? ? ? ? })
  39. ? ? ? ? ? ? router.addRoute(menu)
  40. ? ? ? ? }
  41. ? ? })
  42. ? ? router.addRoute(firstLevelMenu)
  43. }

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

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

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号