Vue router2 没有捕捉到深层嵌套的路由

编程入门 行业动态 更新时间:2024-10-11 15:23:35
本文介绍了Vue router2 没有捕捉到深层嵌套的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的路由结构如下,分为几个文件:

My routes has the following structure divided into several files:

export const router = new VueRouter({
    mode: 'hash',
    base: __dirname,
    saveScrollPosition: true,
    history: true,
    routes : Array.concat(userRoutes, siteRoutes)
})

...
// user/routes.js

export const userRoutes = [
    {
        path: '/user',
        name: 'user_main',
        component: UserMain,
        meta: {authRequired: true},
        children: Array.concat([
            ...
            ], accountRoutes)
    }
]


// account/routes.js 

export const accountRoutes = [
    {
        path: 'account',
        name: 'user_account',
        component: AccountMain,
        children: [
            {path: 'edit',  name: 'user_edit_account', component: EditAccount},
            {path: 'addresses',  name: 'user_addresses',  component: Addresses},
        ]
    }
]

我正在努力抓住

/user/account/addresses

但是对于 account/下的任何内容,我得到的是 AccountMain 组件,而不是我期望的组件.如果我将地址组件从帐户路由中取出并将其移动到说用户/地址它可以工作.但我无法在 AccountMain 下访问.AccountMain下的任何其他组件都是一样的

but for anything under account/ i get the AccountMain component, not the component that i expected. If i take the addresses component out of account routes and move it to say user/addresses it works. but i can not reach under AccountMain. It is same for any other component under AccountMain

如果我尝试访问 account/下不存在的任何内容,例如:

If i try to reach anything that does not exist under account/ for example:

/user/account/blah 

它返回该视图的空页面.

it returns empty page for that view.

但是,添加

beforeEnter: (to, from, next) => {
    to.matched.forEach(m => console.log(m.name)) 
}

到 AccountMain 的路由定义输出一个额外的和预期的名称

to AccountMain's route definition outputs an extra and the expected name

user_main
user_account
user_addresses

因此它找到名称,但返回父组件(AccountMain)而不是它的子组件地址.这与 AccountMain 组件无关,因为如果我从如下所示的路由定义中删除 AccountMain 组件,我仍然无法访问地址并获得一个空页面.

So it finds the name, but returns the parent (AccountMain) instead of it's child component Addresses. This is not related to AccountMain component, since if i remove the AccountMain component from route definition such as the following, i still can not reach the addresses and get an empty page.

export const accountRoutes = [
    {
        path: 'account',
        name: 'user_account',
        children: [
            {path: 'edit',  name: 'user_edit_account', component: EditAccount},
            {path: 'addresses',  name: 'user_addresses',  component: Addresses},

        ]
    }
]

我使用的是 vue 路由器 2.1.1.

I'm using vue router 2.1.1.

我在这里做错了什么?

推荐答案

每个父级都必须返回自己的路由器视图,我遇到了同样的问题,我确实按以下方式修复了它:

Each parent must return it's own router-view , i had the same problem and i did fix it as following :

export default new Router({
  mode: "history",
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    {
      path: "/",
      component: DefaultContainer,
      redirect: "/dashboard",
      children: [
        {
          path: "/administration",
          redirect: "administration/pros",
          name: "Administration",
          component: {
            render(c) {
              return c("router-view");
            }
          },
          children: [
            {
              path: "rules",
              name: "RulesManagement",
              redirect: "rules",
              component: {
                render(c) {
                  return c("router-view");
                }
              },
              children: [
                {
                  path: "",
                  component: RulesManagement
                },
                {
                  path: "edit/:id",
                  name: "Edit Rule",
                  component: EditRule,
                },
                {
                  path: "add/",
                  name: "Add Rule",
                  component: AddRule,
                }
              ]
            } 
          ]
        }
      ]
    }
  ]
});

所以在每个父路由中你必须添加这个:

So in each parent route you have to add this :

component: {
   render(c) {
      return c("router-view");
   }
}

并在它的孩子中添加一个带有 path: ""

and in it's children add a new route with path: ""

希望能帮到大家解决这类问题

I hope it will help you all for this kind of problems

这篇关于Vue router2 没有捕捉到深层嵌套的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 01:25:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   路由   捉到   Vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!