<router-view v-slot="{ Component }" >
<keep-alive>
<component :is="Component " v-if="$route.meta.keepAlive"/>
</keep-alive>
<component :is="Component" v-if="!$route.meta.keepAlive"/>
<!-- <router-view> -->
</router-view>
if(to.name==='goods'&&from.name==='GoodsDetails'){
console.log('缓存',from)
to.meta.keepAlive = true
}
if(to.name === 'GoodsDetails' && from.name === 'goods'){
from.meta.keepAlive = true
console.log('from.name', from)
}
import router from './route/index'
import { getToken,setToken } from './utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'
import { loginCallBack,getUrl } from './api/user'
const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist
router.beforeEach(async(to, from, next) => {
// start progress bar
if(to.name==='goods'&&from.name==='GoodsDetails'){
console.log('缓存',from)
to.meta.keepAlive = true
}
if(to.name === 'GoodsDetails' && from.name === 'goods'){
from.meta.keepAlive = true
console.log('from.name', from)
}
document.title ='丟直播商城'
// set page title
// document.title = getPageTitle(to.meta.title)
// determine whether the user has logged in
const hasToken = getToken()
if (hasToken) {
console.log("hastoken")
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/' })
} else {
// // determine whether the user has obtained his permission roles through getInfo
// const hasRoles = store.getters.roles && store.getters.roles.length > 0
// console.log(store.getters.roles)
// if (hasRoles) {
next()
// } else {
//next({ ...to, replace: true })
// }
}
} else {
/* has no token*/
if(location.href.includes("?code=")){
var code = location.href.split("#")[0].split("=")[1]
loginCallBack({code:code}).then(resp=>{
if(resp.statusCode == 200){
setToken(resp.data.userToken)
console.log('回调成功')
console.log(resp)
location.href="/#/goods"
}else{
location.href="/#/login"
}
})
}else{
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
}
}
}
})
2021-11-08首页添加缓存
最新推荐文章于 2024-04-13 11:29:57 发布
关键词由优快云通过智能技术生成