国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

nuxt中使用路由守衛(wèi)的方法步驟

2019-11-19 12:13:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1.在plugins文件下創(chuàng)建一個(gè)route.js

import { getCookie, setCookie } from '@/pages/logreg/api/cookie'import axios from 'axios'export default ({ app, store }) => { app.router.beforeEach((to, from, next) => {  let isClient = process.client  if (isClient) {   let currentUrl = location.href   if (currentUrl.indexOf('access_token=') !== -1) {    let wechattoken = currentUrl.split('access_token=')[1]    wechattoken = wechattoken.split('&')[0]    setCookie('token', wechattoken, 5)   }   let token = getCookie('token')   if (token) {    store.state.user.userinfo.token = token    axios     .get('https://api.ass.net/pub/api/user_info', {      params: {       token      }     })     .then(res => {      res = res.data      if (res.code == 0) {       res = res.data       res.headImg = res.headImg.replace('http:', 'https:')       store.state.user.userinfo = Object.assign(        {},        store.state.user.userinfo,        res       )      }     })     .catch(error => console.log(error))   }  }  next() })}

2.在nuxt.config.js里面配置

 plugins: [  { src: '@/plugins/route', ssr: true } ],

PS:Nuxt在axios請(qǐng)求攔截中使用路由

最近在開(kāi)發(fā)一個(gè)網(wǎng)站,用的nuxt搭建的框架,因?yàn)樾枰谡?qǐng)求token過(guò)期之后提示用戶(hù)重新登錄并且返回登錄頁(yè)面,但是在axios的配置文件中使用router.push一直報(bào)錯(cuò),都準(zhǔn)備放棄使用公眾組件去進(jìn)行路由跳轉(zhuǎn)了,但是天無(wú)絕人之路,最終在官方文檔中找到了redirect,具體操作如下:

在axios的js文件中添加默認(rèn)的方法,并且獲取redirect,并且使用使用myredirect將redirect儲(chǔ)存起來(lái)

let myredirect;export default function ({redirect }) { myredirect = redirect;}

在需要使用路由跳轉(zhuǎn)的地方進(jìn)行跳轉(zhuǎn)(此處在判斷token過(guò)期時(shí)跳轉(zhuǎn))

if (error.message.toString().slice(-3) === '401') {  Vue.prototype.$message.error('登陸超時(shí),請(qǐng)重新登陸...')  setTimeout(function () {   return myredirect('/login/login')  }, 2000) }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 利津县| 瓦房店市| 榕江县| 泗水县| 花垣县| 云安县| 大理市| 屯留县| 宿松县| 毕节市| 朝阳区| 双江| 台中县| 德安县| 寿光市| 嵊泗县| 沭阳县| 宁安市| 渑池县| 常宁市| 屯留县| 虞城县| 贵州省| 饶平县| 秦安县| 沁源县| 永嘉县| 孝义市| 海林市| 讷河市| 原平市| 和龙市| 衡阳县| 密云县| 类乌齐县| 彭州市| 右玉县| 东乌珠穆沁旗| 天津市| 阜平县| 朝阳县|