vue 路由嵌套(子路由跳转报错或者失效解决方法)

本文探讨了在Vue.js应用中遇到的嵌套路由懒加载失效的问题,指出子路由的定义和引用方式可能导致加载失败。建议在定义路由时确保组件正确引用,并提供了一个示例结构。同时,当遇到模板编译错误时,可能需要在项目配置中启用`runtimeCompiler`。此外,强调了子路由路径不应以`/`开头,以及懒加载组件的正确导入方式。
摘要由CSDN通过智能技术生成

嵌套路由时,子路由懒加载有时会失效,最好定义引用好,再进行加载,示例结构:

{
    path: '/shopRech',
    redirect:'/shopRech/shopRechHome',
    component:{
      template:'<div> <router-view></router-view></div>'
    },
    meta: { title: '积分充值'},
    children:[
      {
        path: 'shopRechHome',
        name:'shopRechHome',
        component:home ,
        meta: { title: '积分充值'}
      },
      {
        path: 'rechargeInfo',
        name:'rechargeInfo',
        component:shopRechHome,
        meta: { title: '积分充值'}
      },
    ]
  },

如果是直接跳转,不是父页面>加载<router-view></router-view>,那么结构如上面例子所示。如果写入报错 You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.那么在你的项目配置项,以vue.cli3为例,在vue.config.js里面写入runtimeCompiler: true配置
如果 component:{template:'<div> <router-view></router-view></div>'},不起作用尝试 component: { render(c) {return c('router-view') }},在这里插入图片描述注意:子路由path不要写 “/” 。写入 / 会跳不到或者报错。子路由component引入,先定义再引入,懒加载component: () => import('@/views/commodityManagement/goodsRule/index.vue'),会失效。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 2 的嵌套路由可以帮助我们构建复杂的页面结构。在嵌套路由中,一个路由可以包含多个路由,而每个路由又可以再次包含自己的路由。 下面是一个示例,展示了如何在 Vue 2 中使用嵌套路由: 首先,我们需要设置主路由(父路由),这可以在 `router/index.js` 文件中完成: ```javascript import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) export default new Router({ routes: [ { path: '/', component: Home, children: [ { path: 'about', component: About, children: [ { path: 'contact', component: Contact }, // 其他路由... ] }, // 其他路由... ] } ] }) ``` 在上面的示例中,我们定义了一个主路由 `/`,它的组件是 `Home`。然后,我们定义了一个路由 `/about`,它的组件是 `About`。在 `About` 组件中,又定义了一个路由 `/contact`,它的组件是 `Contact`。 现在,我们可以在 `Home` 组件的模板中添加一个 `<router-view>` 标签来展示嵌套路由的内容: ```html <template> <div> <h1>Home</h1> <router-view></router-view> </div> </template> ``` 同样地,在 `About` 组件的模板中也要添加一个 `<router-view>` 标签: ```html <template> <div> <h2>About</h2> <router-view></router-view> </div> </template> ``` 这样,当我们访问 `/about` 路径时,`Home` 组件会显示出来,并且在其中展示 `About` 组件。 当访问 `/about/contact` 路径时,`Home` 和 `About` 组件都会显示出来,并且在 `About` 组件中展示 `Contact` 组件。 希望这个示例对你有所帮助!如果你还有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值