1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/coderd-repos-eva

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Это зеркальный репозиторий, синхронизируется ежедневно с исходного репозитория.
Клонировать/Скачать
main.js 3.7 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
Caesar Отправлено 3 лет назад e492eae
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import './assets/style/element-variables.scss'
import VueClipboard from 'vue-clipboard2'
import directives from './directives'
import filters from './filters'
import plugins from './plugins'
import { mapState, mapMutations } from 'vuex'
import { fetchMenuTree } from './api/system/menu'
Vue.config.productionTip = false
Vue.use(ElementUI, {
size: 'small'
})
Vue.use(VueClipboard)
Vue.use(directives)
Vue.use(filters)
Vue.use(plugins)
new Vue({
data: {
loading: false
},
router,
store,
computed: {
...mapState(['userInfo', 'homePage'])
},
watch: {
async userInfo () {
if (this.userInfo == null) {
return
}
await this.initRoutes()
}
},
methods: {
...mapMutations(['switchCollapseMenu', 'setHomePage']),
/**
* 初始化本地配置
*/
initLocalConfig () {
// 菜单状态配置
const menuStatus = window.localStorage.getItem('MENU_STATUS')
if (menuStatus != null) {
this.switchCollapseMenu(menuStatus === 'true')
}
},
/**
* 初始化路由
*
* @returns {Promise<void>}
*/
async initRoutes () {
if (this.loading || this.userInfo == null) {
return
}
this.loading = true
// 重置菜单
this.$store.commit('resetMenus')
// 获取菜单
const storeMenus = this.$store.state.menuData.list
if (storeMenus.length > 0 && this.homePage == null) {
this.setHomePage(storeMenus[0])
}
await fetchMenuTree()
.then(menus => {
// 添加菜单
storeMenus.push.apply(storeMenus, menus)
// 添加路由
this.__addRouters(storeMenus)
// 404
router.addRoute({
path: '*',
redirect: '/not-found'
})
// 首页
router.addRoute({
name: 'index',
path: '/',
redirect: this.homePage.url
})
// 路由加载完成后,如果访问的是/,跳转至动态识别的首页
if (this.$route.path === '/') {
this.$router.push(this.homePage.url)
}
})
.catch(e => {
throw e
})
.finally(() => {
this.loading = false
})
},
/**
* 新建路由
*
* @param routes 需添加的路由
* @param parents 需添加到的目标列表
* @private
*/
__addRouters (routes, parents = []) {
if (routes == null || routes.length === 0) {
return
}
const rs = router.getRoutes()
for (const route of routes) {
const parentsDump = JSON.parse(JSON.stringify(parents))
parentsDump.push(route)
if (route.url == null || route.url === '') {
this.__addRouters(route.children, parentsDump)
continue
}
if (rs.findIndex(r => r.path === route.url) > -1) {
this.__addRouters(route.children, parentsDump)
continue
}
if (this.homePage == null) {
this.setHomePage(route)
}
router.addRoute('layout', {
path: route.url,
name: route.label,
meta: {
title: route.label,
paths: [...parents.map(p => p.label), route.label]
},
component: () => import('@/views' + route.url)
})
this.__addRouters(route.children, parentsDump)
}
}
},
async created () {
if (this.userInfo == null) {
return
}
await this.initRoutes()
.catch(() => {})
},
mounted () {
this.initLocalConfig()
},
render: h => h(App)
}).$mount('#app')

Комментарий ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/coderd-repos-eva.git
git@gitlife.ru:oschina-mirror/coderd-repos-eva.git
oschina-mirror
coderd-repos-eva
coderd-repos-eva
master