默认路由名字使用symbol,防止命名冲突

This commit is contained in:
Jason 2022-06-23 11:41:08 +08:00
parent eddc1db493
commit e9f9b2c035
4 changed files with 16 additions and 11 deletions

View File

@ -19,7 +19,7 @@ export default defineComponent({
onMounted(async () => {
//
const data = await store.dispatch('app/getConfig')
console.log('data', data)
console.log('data', data)
// logo
let favicon: HTMLLinkElement = document.querySelector('link[rel="icon"]')!
if (favicon) {
@ -39,6 +39,6 @@ export default defineComponent({
</script>
<style lang="scss">
@import './assets/font/iconfont.css';
@import './styles/index.scss';
@import "./assets/font/iconfont.css";
@import "./styles/index.scss";
</style>

View File

@ -1,5 +1,5 @@
import { RouteRecordRaw, RouterView } from "vue-router"
import Layout from '@/layout/index.vue'
export enum MenuType{
Catalogue = 'M',
Menu = 'C',
@ -12,6 +12,7 @@ const modules = import.meta.glob('/src/views/**/*.vue')
// 过滤路由所需要的数据
export function filterAsyncRoutes(routes: any[], firstRoute = true) {
return routes.map((route => {
const routeRecord = createRouteRecord(route, firstRoute)
if (route.children != null && route.children && route.children.length) {
@ -39,7 +40,7 @@ export function createRouteRecord(route: any, firstRoute: boolean): RouteRecordR
}
switch (route.menuType) {
case MenuType.Catalogue:
routeRecord.component = RouterView
routeRecord.component = firstRoute ? Layout : RouterView
break
case MenuType.Menu:
routeRecord.component = loadRouteView(route.component)

View File

@ -4,7 +4,7 @@
import NProgress from 'nprogress'
import store from './store'
import router from './router'
import router, { indexName } from './router'
import 'nprogress/nprogress.css'
// NProgress配置
@ -27,7 +27,11 @@ router.beforeEach(async (to, from, next) => {
await store.dispatch('user/getUser')
const routes = await store.dispatch('permission/generateRoutes')
routes.forEach((route: any) => {
router.addRoute('index', route) // 动态添加可访问路由表
if(!route.children) {
router.addRoute(indexName, route)
return
}
router.addRoute(route) // 动态添加可访问路由表
})
console.log(router.getRoutes())
if (to.path === '/login') {

View File

@ -1,5 +1,6 @@
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/index.vue'
export const indexName = Symbol('index')
/**
* Note: 路由配置项
*
@ -18,12 +19,11 @@ import Layout from '@/layout/index.vue'
// 公共路由
export const constantRoutes: Array<RouteRecordRaw> = [
{
name: indexName,
path: '/',
redirect: 'workbench',
name: 'index',
component: Layout,
redirect: '/workbench',
component: Layout
},
{
path: '/permission',
component: Layout,