如何在Vue主组件App.vue外加载vue-router指定的Dashboard路由
解决方案
核心思路
所有路由默认会渲染在App.vue的<router-view>节点下,继承App.vue的全局样式。你需要将路由拆分为两个分组,一组使用带公共样式的默认布局(首页、登录、注册页),另一组使用无公共样式的空布局(Dashboard全链路页面),避免样式继承。
方案1:App.vue动态渲染布局(改动最小)
无需调整现有路由结构,只需两步即可实现:
- 给Dashboard根路由添加meta标记,标识该路由不需要使用App.vue的公共样式
// 路由配置中找到Dashboard根路由,添加meta字段 { path: '/dash', name: 'dashboardd', meta: { noGlobalLayout: true }, // 新增这一行 component: () => import('@/views/dashboard/Index'), children: [/* 原有子路由保持不变 */] }
- 修改App.vue的模板,根据当前路由的meta值判断是否渲染公共样式部分
<template> <!-- 非Dashboard路由渲染原有公共结构(导航栏、全局容器等带样式的内容) --> <div v-if="!$route.meta.noGlobalLayout"> <!-- 这里保留你原来App.vue里的所有公共内容 --> <router-view /> </div> <!-- Dashboard路由直接渲染内容,不包裹任何App.vue的公共结构 --> <router-view v-else /> </template>
Dashboard的所有子路由会自动继承父路由的meta属性,不会被App.vue的公共样式影响。
方案2:分层布局路由(可维护性更高)
如果后续还有其他需要独立布局的页面,推荐用路由分层的方案:
- 新建两个布局组件:
@/layouts/DefaultLayout.vue:把原来App.vue里的所有公共内容、样式都移到这个组件,最后放<router-view>@/layouts/EmptyLayout.vue:只有一个<router-view>节点,无任何公共样式和结构
- 调整完整路由配置如下:
import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) export default new Router({ mode: 'history', base: process.env.BASE_URL, routes: [ // 默认布局分组:带公共样式,适用于首页、登录、注册 { path: '/', component: () => import('@/layouts/DefaultLayout.vue'), children: [ { name: 'Home', path: '', component: () => import('@/components/Home.vue'), }, { name: 'Login', path: '/login', component: () => import('@/components/Login.vue'), }, { name: 'Register', path: '/register', component: () => import('@/components/Register.vue'), } ] }, // 空布局分组:无公共样式,适用于Dashboard全链路页面 { path: '/dash', component: () => import('@/layouts/EmptyLayout.vue'), children: [ { path: '', name: 'dashboardd', component: () => import('@/views/dashboard/Index'), children: [ //CLIENTS { name: 'Clients', path: '/Clients', component:()=> import('@/views/clientss/Clientss') }, //SALES { name: 'Sales', path: '/sales', component:()=> import('@/views/sales/Sales') }, //NUEVA VENTA { name: 'Sales', path: '/new-sale', component:()=> import('@/views/sales/NewSale') }, //productos { name: 'Productos', path: '/products', component:()=> import('@/views/articulos/Products') }, //listar articulos { name: 'ListarArticulos', path: '/articulos', component:()=> import('@/views/articulos/ListarArticulos') }, //crear articulo { name: 'CrearArticulo', path: '/articulos/crear', component:()=> import('@/views/articulos/CrearArticulo') }, //editar articulo { name: 'EditarArticulo', path: '/articulos/editar/:id', component:()=> import('@/views/articulos/EditarArticulo') }, // Dashboard { name: 'Dashboard', path: '/dash', component: () => import('@/views/dashboard/Dashboard'), }, { name: 'PROVEEDORES', path: '/providers', component: () => import('@/views/providers/Provider'), }, { name: 'USUARIOS', path: '/users', component: () => import('@/views/users/User'), }, { name: 'REPORTES', path: '/reports', component: () => import('@/views/reports/Report'), }, // Pages { name: 'User Profile', path: 'pages/user', component: () => import('@/views/dashboard/pages/UserProfile'), }, { name: 'Notifications', path: 'components/notifications', component: () => import('@/views/dashboard/component/Notifications'), }, { name: 'Icons', path: 'components/icons', component: () => import('@/views/dashboard/component/Icons'), }, // Maps { name: 'Google Maps', path: 'maps/google-maps', component: () => import('@/views/dashboard/maps/GoogleMaps'), }, ] } ] }, ], })
修改完路由后,把App.vue的内容清空,只保留一个<router-view>节点即可,所有布局逻辑都由路由层控制,后续新增独立布局页面只需新增对应布局分组即可。
注意事项
如果你的App.vue的style标签没有加scoped属性,里面写的全局样式依然会影响所有页面,建议把公共样式移到DefaultLayout组件内,或者给Dashboard的根容器加专属的样式作用域,隔离全局样式影响。
内容的提问来源于stack exchange,提问作者Marcos Selin
相关产品推荐
相关产品推荐

