vue.js - 嵌套路由未在路由器 View 中呈现

标签 vue.js vue-router nested-routes

我正在设置一个大型系统,所以我希望每个模块都有分散的 router.js 文件。他们每个人都有路由数组声明,这些声明将被推送到主路由数组。

所以现在我有一种路由文件的根,它有 VueRouter 实例和设置,它导入第一级 route.js 文件来构建路由数组。

主要的routes.js文件

import DashboardRoutes from './views/dashboard/routes'

Vue.use(Router)

export default new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {// Root public site
      path: '/',
      component: () => import('./views/pages/Index'),
      children: PagesRoutes
    },
      name: 'page_error',
      path: '*',
      component: () => import('./views/error/Error')
    }
  ].concat(
    DashboardRoutes
  )
})

模块(一级)routes.js文件

import InventoryRoutes from './inventarios/routes'

const routes = {
  path: '/panel',
  component: () => import('./Index'), // This Index has a layout with a router-view in it
  children: [
    // Inicio del panel
    {
      name: 'dashboard',
      path: '',
      component: () => import('./Dashboard'),
    }
  ].concat(
    InventariosRoutes
  )
}

export default routes

组件(二级)routes.js文件

const routes = {
  path: 'inventario',
  name: 'panel_inventario',
  component: { template: '<router-view/>' },
  children: [
    // Productos y servicios
    {
      name: 'panel_inventarios_productos-servicios',
      path: 'productos-servicios',
      component: () => import('./productos-servicios/ProductosServiciosHome'),
    },

  ]
}
export default routes

vue-tools 的组件树中,我看到一个 AnnonymousComponent 在我 child 的 router-view 应该在的地方。

更新

我只是创建了一个外部组件来命名它并检查它是否被渲染成这样

组件(二级)routes.js文件

const loader = {
  template: `
    <div class="InventarioLoader">
      <router-view></router-view>
    </div>
  `,
  name: 'InventarioLoader'
}

const routes = {
  path: 'inventario',
  name: 'panel_inventario',
  component: loader,
  children: [
    // children go here
  ]
}

现在我看到了我的 InventarioLoader 组件,但我仍然没有看到我的子组件在其中呈现

更新

我在控制台上看到这个错误

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.

最佳答案

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 不编译字符串模板。这是出于性能原因。

Since the runtime-only builds are roughly 30% lighter-weight than their full-build counterparts, you should use it whenever you can

参见 https://v2.vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only

要么创建一个真正的单文件组件,要么使用渲染函数

import Vue from 'vue'

const RouterView = Vue.component('router-view')

const loader = {
  name: 'InventarioLoader',
  render (createElement) {
    return createElement(RouterView)
  }
}

关于vue.js - 嵌套路由未在路由器 View 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58312044/

相关文章:

javascript - 除了计算之外的任何其他方式来存储 Vue 路由参数,使得引用无法修改

vue.js - VueJS 路由器守卫实现访问控制机制

javascript - VueJS路由相同的组件不触发OWL Carousel

javascript - ExpressJS 中的嵌套路由

嵌套路由上的 Django REST 权限

javascript - 在离开过渡处于事件状态时继续渲染组件

javascript - v-if 指令中允许的查找级别是多少?

javascript - 检查组件是否附加了事件监听器

vue.js - 如何使用 Vuejs 插入 url

ruby-on-rails - 嵌套资源并限制创建的路由 :only and :except