router.beforeEach((to, from, next) => { // 設置全局店鋪ID shopid const shopid = from.query.shopid // 如果沒有shopid 先跳轉到總店 shopid為1 if (!shopid && !to.query.shopid) { router.replace({ path: '/', query: { 'shopid': 1 } }) return false } // 此處通過修改push給query增加shopid的方式, 會重置跳轉數據. // 這會影響到replace跳轉, 導致變成正常push跳轉. // 如果原跳轉是replace方式, 需要在query里主動添加shopid. 避免被重置數據. if (shopid && (shopid !== to.query.shopid)) { // 如果傳遞了shopid 走正常路由 // 否則先手動添加shopid參數 if (!to.query.shopid) { to.query.shopid = shopid router.push({ path: to.path, query: to.query }) return false } } // 設置頁面標題 const title = to.meta && to.meta.title if (title) { document.title = title } // 是否登錄 if (!store.get('token')) { // 需要登錄 if (to.meta && to.meta.requiresAuth) { next({ path: '/', query: { 'shopid': 1 }, replace: true }) } else { next() } } else { next() } })