Vue3动态路由解决白屏问题

1. 在Nav组件中调用添加动态路由的方法

Vue3动态路由解决白屏问题_第1张图片

Vue3动态路由解决白屏问题_第2张图片

 解决首次进入页面,刷新白屏

1. 在main.js中调用动态路由的方法

Vue3动态路由解决白屏问题_第3张图片

 

import { createApp } from 'vue'
import App from '@/App.vue'
import router from '@/router/index'
import 'element-plus/dist/index.css'
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import dynamicRoute from '@/utils/dynicRoute'
import pinia from '@/store'
const app = createApp(App)
// elementUI的icon
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.use(pinia)
app.use(ElementPlus)
dynamicRoute()
app.use(router)
app.mount('#app')

解决首次登录,token失效问题:

1. 在store仓库中的getters属性中添加判断

Vue3动态路由解决白屏问题_第4张图片

 

getters:{
        getDynicRoute(state:StateType){
            let homeRouteObj:RouteRecordRaw = {
                path:'/home',
                component:()=>import('@/views/HomePage.vue'),
                children:[]
            }
            let ary:Array = []           
            if(state&&state.permissionList){
            state.permissionList.forEach(item=>{
                if(item.children){
                    item.children.forEach(subItem=>{
                        let routeItem ={
                            path:subItem.path,
                            component:getViews((`../views${subItem.path}.vue`))
                        }
                        ary.push(routeItem)
                    })
                }
            })
           }
           homeRouteObj.children = ary
           return homeRouteObj
        }
    },

你可能感兴趣的:(vue.js,javascript,前端)