【Vue】Router路由无法跳转问题整理

问题集

整理了部分Vue Router路由无法跳转问题:

  1. 顶层router-view只能被顶层路由配置内容使用:此问题异常表现在路由跳转但页面不变
  2. 子路由跳转必需父路由对应的组件中存在router-view:此问题异常表现在路由跳转但页面不变
    1. 子路由配置路径会自动继承父路径并自动增加/
    2. 如果子路径配置路径存在前缀/,则代表为全路径,需要包含父路由路径
  3. 跳转路由与当前路由相同时,即重复路由时,会触发NavigationDuplicated错误

顶层路由视图只能顶层配置使用

<!-- App.vue -->
<template>
  <div id="app">
    <router-view/> <!--顶层路由视图-->
  </div>
</template>

父组件

子层路由视图,只能子路由配置可以使用,比如/parent路由中的children子路由配置child
换句话说:子路由可以跳转则必需对应父路由的组件中村啊在<router-view/>

<!-- Parent.vue -->
<template>
  <div id="parent">
    Parent Content
    <router-view/> <!--关键点:子层路由视图-->
  </div>
</template>

子组件

<!-- Child.vue -->
<template>
  <div id="child">
      Child Content
  </div>
</template>

路由配置

  1. 一级路由只能用在在顶层路由视图,如name=Home,Parent等路由只能用在App.vue中的<router-view/>
  2. 子路由只能用在父组件中的<router-view/>中,如name=Child路由只能用在Parent.vue中的<router-view/>
  3. 如果Child组件中仍有<router-view/>,并且需要使用,
    1. 则可以在name=Child路由中继续配置children子路由
    2. 或者配置新的和’name=Parent’同级别的路由配置依赖Child.vue组件,如name=NewChild,并在其中配置children子路由
// router配置
export default new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      redirect:'/parent', // 可以使用App.vue中的顶层路由视图
    },
    {
      path: '/parent',
      name: 'Parent',
      component: Parent,
      children: [
        {
          path: 'child', // 非全路径配置时,子路径开头无需`/`,只需子路径`child`
          // path : '/parent/child', // 也可以配置全路径
          name: 'Child',
          component: Child,
        //   children: [] // 如果Child组件中仍有<router-view/>,则可以继续配置子路由
        },
      ],
    },
    {
      path: '/new-child',
      name: 'NewChild',
      component: Child,
    //   children: [] // 如果Child组件中仍有<router-view/>,则可以继续配置子路由
    },
  ],
})

子路由路径问题

  1. 子路由路径以/开头代表全路径配置,需要包含父路由路径,如path:'/parent/child'
  2. 子路由可省略/开头,自动继承父路由路径,如path:'child'
    上面也有代码说明也有介绍
// router配置
export default new Router({
  routes: [
    {
      path: '/parent',
      name: 'Parent',
      component: Parent,
      children: [
        {
          path: 'child', // 非全路径配置时,子路径开头无需`/`,只需子路径`child`
          // path : '/parent/child', // 也可以配置全路径
          name: 'Child',
          component: Child,
        },
      ],
    },
  ],
})

路由重复问题

vue-router 不知道哪个版本开始的问题,小编没关心过这个,解决内容可以参考.

当准备跳转的路由是当前路由是,即假如当前路由时/parent,仍旧执行this.$router.push('/parent')就会报类似以下错误:
请添加图片描述

NavigationDuplicated: Avoided redundant navigation to current location: "/data-manage".
    at createRouterError (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2053:15)
    at createNavigationDuplicatedError (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2023:15)
    at HTML5History.confirmTransition (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2340:18)
    at HTML5History.transitionTo (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2267:8)
    at HTML5History.push (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:2613:10)
    at eval (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:3043:24)
    at new Promise (<anonymous>)
    at VueRouter.push (webpack-internal:///./node_modules/vue-router/dist/vue-router.esm.js:3042:12)
    at VueComponent.goto (webpack-internal:///./node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/commons/layout/SideBar.vue:24:26)
    at click (webpack-internal:///./node_modules/vue-loader/lib/template-compiler/index.js?{"id":"data-v-3cb2454e","hasScoped":false,"transformToRequire":{"video":["src","poster"],"source":"src","img":"src","image":"xlink:href"},"buble":{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?type=template&index=0!./src/components/commons/layout/SideBar.vue:17:30)

解决方法:

  1. 重写vue-routerpush方法
  2. 捕获异常并忽略:当然也需要自己定义一个统一的push方法用来替换使用this.$router.push
  3. 自记录当前路径和要跳转的路径,如果当前路径和要跳转的路径相同,则不跳转

重写push方法

router/index.js(在自己项目的路由配置中哈,不要非要较真~)重写VueRouter.push方法

import VueRouter from 'vue-router'
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
    return VueRouterPush.call(this, to).catch(err => err)
}

捕获异常并忽略

this.$router.push(url).catch(() => {})

推荐提取为统一公共方法,如:

export const routerPush = (url) => {this.$router.push(url).catch(() => {})}

判断路径是否为当前路径

如果路径非当前路径才允许跳转,否则不跳转,同样推荐提取为统一公共方法

// currentUrl存储在内存中
if (this.$route.path !== currentUrl) {
    this.$router.push({ path: currentUrl })
  }
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue RouterVue.js 官方的路由管理器,用于实现前端路由跳转。要进行路由跳转,你需要完成以下几个步骤: 1. 首先,确保你的项目已经安装了 Vue Router。可以通过 npm 或 yarn 进行安装: ```bash npm install vue-router ``` 或 ```bash yarn add vue-router ``` 2. 在你的 Vue 项目的入口文件(一般是 `main.js`)引入 Vue Router,并使用它: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' // 导入你的路由配置文件 import routes from './routes' Vue.use(VueRouter) const router = new VueRouter({ mode: 'history', // 可选值为 'hash' 或 'history',默认为 'hash' routes // 路由配置 }) new Vue({ router, // 注入路由实例 render: h => h(App) }).$mount('#app') ``` 3. 创建一个路由配置文件(例如 `routes.js`),在该文件定义路由的映射关系: ```javascript import Home from './views/Home.vue' import About from './views/About.vue' const routes = [ { path: '/', name: 'home', component: Home }, { path: '/about', name: 'about', component: About } ] export default routes ``` 4. 在你的 Vue 组件使用 `<router-link>` 标签或 `$router` 对象进行路由跳转。下面是几个常用的示例: - 使用 `<router-link>` 标签实现路由跳转: ```html <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> ``` - 使用 `$router` 对象编程式地进行路由跳转: ```javascript // 在某个方法跳转到指定路由 this.$router.push('/') // 跳转到根路径 this.$router.push('/about') // 跳转到 /about 路径 // 在某个方法通过路由名称跳转 this.$router.push({ name: 'home' }) // 跳转到名称为 home 的路由 ``` 这样,你就可以通过 Vue Router 实现路由跳转了。请注意,以上只是一个简单的示例,你可以根据实际需要配置更多高级功能,例如路由参数、嵌套路由等。详情请参考 Vue Router 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值