2021-11-08首页添加缓存

<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}`)
  
      }
    }
  }
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值