如何为子路由器设置默认路由器并在子路由中设置404页面?

时间:2019-04-03 09:36:52

标签: vue.js vuejs2 vue-router

我有包含子路由器的主页。在那个路由器中,我有默认的路由器(目标组件)作为主页。但是当我添加404页面时,我的子路由器一直都设置为404页面。

如何正确设置子默认路由器并设置404页面?

我的路由器

{
      path: '/',
      name: 'MainPage',
      component: MainPage,
        meta: {
            title: (route: any) => 'Goal list'
        },
        children: [
            {
                path: '/404',
                name: '404',
                component: NotFound,
            }, {
                path: '*',
                redirect: '/404'
            },
            {
                path: '',
                name: 'Goals',
                component: Goals,
                meta: {
                    title: (route: any) => route.name
                }
            },
            {
                path: '/profile',
                name: 'Profile',
                component: Profile,
                meta: {
                    title: (route: any) => route.name
                }
            },
            {
                path: '/goal-:id',
                name: 'Goal',
                component: Goal,
                meta: {
                    title: (route: any) => route.name
                }
            }
        ],
    },

1 个答案:

答案 0 :(得分:1)

尝试:

像这样:

{
      path: '/',
      name: 'MainPage',
      component: MainPage,
        meta: {
            title: (route: any) => 'Goal list'
        },
        children: [
            {
                path: '',
                name: 'Goals',
                component: Goals,
                meta: {
                    title: (route: any) => route.name
                }
            },
            {
                path: 'profile',
                name: 'Profile',
                component: Profile,
                meta: {
                    title: (route: any) => route.name
                }
            },
            {
                path: 'goal-:id',
                name: 'Goal',
                component: Goal,
                meta: {
                    title: (route: any) => route.name
                }
            },
            {
                path: '404',
                name: '404',
                component: NotFound,
            },
            {
                path: '*',
                redirect: '/404'
            },
        ],
    },