为了账号安全,请及时绑定邮箱和手机立即绑定

如何在两个不同的子路由中显示一个相同的 Vue 组件?

如何在两个不同的子路由中显示一个相同的 Vue 组件?

慕运维8079593 2022-05-26 16:59:10
const routes = [{    path: '/',    component: LogInView},{    path: '/store',    component: Dashboard,    children: [        {            path: '/products',            component: ProductsView,        },    ]},{    path: '/platform',    component: Dashboard,    children: [        {            path: '/products',            component: ProductsView,        },    ]},{    path: '/platform',    component: Dashboard} ]假设,我正在尝试为&渲染Dashboard组件,并为&这两个路由渲染组件。但问题是当我点击或url 时,它是渲染组件。/store/platformProductView/store/products/platform/products/store/products/platform/productsDashboard
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

您的代码的问题是您在每个子路由('/products')删除它之前写了一个斜杠



查看完整回答
反对 回复 2022-05-26
?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

在您的代码中,您仍然有一条component路线children:


{

    path: '/store',

    component: Dashboard,

    children: [

        {

            path: '/products',

            component: ProductsView,

        },

    ]

},

但是,Vuejs 文档显示父路由中不应该有组件,如下所示:


const router = new VueRouter({

  routes: [

    { path: '/user/:id', component: User,

      children: [

        {

          // UserProfile will be rendered inside User's <router-view>

          // when /user/:id/profile is matched

          path: 'profile',

          component: UserProfile

        },

        {

          // UserPosts will be rendered inside User's <router-view>

          // when /user/:id/posts is matched

          path: 'posts',

          component: UserPosts

        }

      ]

    }

  ]

})

所以,相反,你应该有:


const routes = [

{

    path: '/',

    component: LogInView

},

{

    path: '/store',

    // component: Dashboard, // <-- should be removed

    children: [

        {

            path: 'products',

            component: ProductsView,

        },

    ]

},

{

    path: '/platform',

    children: [

        {

            path: 'products',

            component: ProductsView,

        },

    ]

},

{

    path: '/platform',

    component: Dashboard

} ]

@TEFO正确地指出子路由不应包含斜杠


查看完整回答
反对 回复 2022-05-26
  • 2 回答
  • 0 关注
  • 166 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号