8 changed files with 19 additions and 100 deletions
@ -1,54 +1,11 @@ |
|||
import { createRouter, createWebHashHistory } from 'vue-router' |
|||
import { staticRoutes } from './routes' |
|||
import { useStore } from '@/store/root' |
|||
import { getIndexList, getQMGoShopData } from '@/http/api' |
|||
import { ERR_OK } from '@/http/config' |
|||
|
|||
const router = createRouter({ |
|||
history: createWebHashHistory(process.env.BASE_URL), |
|||
routes: staticRoutes |
|||
}) |
|||
|
|||
function dynamicRoutes(list) { |
|||
return { |
|||
path: '/index', |
|||
name: 'home', |
|||
component: list.length ? () => import(/* webpackChunkName: "Home" */ '@/views/Index/Index') : () => import(/* webpackChunkName: "Home" */ '@/views/Guide/Guide'), |
|||
|
|||
meta: { |
|||
showMenu: true, |
|||
showMap: list.length ? false : true |
|||
} |
|||
} |
|||
} |
|||
|
|||
router.beforeEach(async to => { |
|||
try { |
|||
const store = useStore() |
|||
|
|||
const res = await getIndexList() |
|||
const { data, code } = res |
|||
if (code === ERR_OK) { |
|||
if (!router.hasRoute('home')) { |
|||
const { |
|||
data: { searchList, recommendList } |
|||
} = await getQMGoShopData() |
|||
store.SET_INDEX_LIST({ ...data, recommendList, hotSearch: searchList }) |
|||
|
|||
router.addRoute({ |
|||
path: '/index', |
|||
name: 'home', |
|||
component: import(/* webpackChunkName: "Home" */ '@/views/Index/Index') |
|||
}) |
|||
|
|||
return to.fullPath |
|||
} |
|||
} |
|||
} catch (error) { |
|||
return to.fullPath |
|||
} |
|||
}) |
|||
|
|||
export function initRouter(app) { |
|||
app.use(router) |
|||
} |
|||
|
|||
Loading…
Reference in new issue