最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue中使用axios固定url請(qǐng)求前綴

 更新時(shí)間:2022年12月09日 10:29:09   作者:Agwenbi  
這篇文章主要介紹了vue中使用axios固定url請(qǐng)求前綴的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用axios固定url請(qǐng)求前綴

main.js中添加:

使用方法:

定義axios默認(rèn)路徑前綴或動(dòng)態(tài)修改前綴

如:每個(gè)請(qǐng)求url前都要加一個(gè)前綴,但會(huì)根據(jù)開(kāi)發(fā)環(huán)境不同而變化,那么我們可以寫(xiě)一個(gè)方法去引用,方便后面維護(hù)

.env.development 開(kāi)發(fā)文件中寫(xiě)入要用的服務(wù)編碼

# 微服務(wù)編碼
VUE_APP_SERVICE_PREFIX = '/0201010254'

src/settings.js 新建的settings文件中引入

module.exports = {
? /**
? ?* 主站標(biāo)題
? ?* @type {string}
? ?*/
? title: '開(kāi)發(fā)項(xiàng)目名稱(chēng)',
?
? /**
? ?* @type {boolean} true | false
? ?* @description Whether fix the header
? ?*/
? fixedHeader: false,
?
? /**
? ?* @type {string | array} 'production' | ['production', 'development']
? ?* @description Need show err logs component.
? ?* The default is only used in the production env
? ?* If you want to also use it in dev, you can pass ['production', 'development']
? ?*/
? errorLog: 'production',
? /**
? ?* base API
? ?*/
? baseApi: process.env.VUE_APP_BASE_API,
? /**
? ?* 服務(wù)編碼
? ?*/
? servicePrefix: process.env.VUE_APP_SERVICE_PREFIX,
?
}

之后請(qǐng)求文件中引入

新建api/app.js 封裝axios請(qǐng)求并引用自定義的服務(wù)編碼文件

// 應(yīng)用層封裝接口
import request from '@/utils/request'
import settings from '@/settings'
?
// 獲取配置 不需替換
export function getAppConfig(params) {
? return request({
? ? url: settings.servicePrefix + '/config',
? ? method: 'get',
? ? params
? })
}

其中的request文件是引用自建的請(qǐng)求攔截文件,根據(jù)各自需求

import axios from 'axios'
import store from '@/store'
import { getToken, removeToken } from '@/utils/auth'
?
const BASE_URL = process.env.VUE_APP_BASE_API
?
// create an axios instance
const service = axios.create({
? // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
? // withCredentials: true, // send cookies when cross-domain requests
? timeout: 60 * 1000 // request timeout
})
?
// request interceptors
service.interceptors.request.use(
? config => {
? ? // do something before request is sent
? ? // 設(shè)置baseURL
? ? config.baseURL = config.url.startsWith('/mock') ? '' : BASE_URL
? ? const token = getToken()
? ? if (!config.noAuth && token) {
? ? ? // let each request carry token
? ? ? config.headers['Authorization'] = token
? ? }
? ? if (store.state.user.info && store.state.user.info.comId) {
? ? ? config.headers['comId'] = store.state.user.info.comId
? ? ? config.headers['comCode'] = store.state.user.info.comCode
? ? ? config.headers['groupUserCode'] = store.state.user.info.groupUserCode
? ? ? config.headers['userCode'] = store.state.user.info.userCode
? ? }
? ? return config
? },
? error => {
? ? // do something with request error
? ? return Promise.reject(error)
? }
)
?
// response interceptor
service.interceptors.response.use(
? /**
? ?* If you want to get http information such as headers or status
? ?* Please return ?response => response
? ?*/
?
? /**
? ?* Determine the request status by custom code
? ?* Here is just an example
? ?* You can also judge the status by HTTP Status Code
? ?*/
? response => {
? ? if (response.headers.newjwt) {
? ? ? store.dispatch('user/setToken', response.headers.newjwt)
? ? }
? ? if ((response.data.status && +response.data.status === 16) || response.status === 401) {
? ? ? if (response.status === 401) {
? ? ? ? return Promise.reject('沒(méi)有接口權(quán)限,請(qǐng)聯(lián)系管理員')
? ? ? } else {
? ? ? ? setTimeout(() => {
? ? ? ? ? // 清除登錄狀態(tài)
? ? ? ? ? removeToken()
? ? ? ? ? window.location.href = '/'
? ? ? ? }, 1000)
? ? ? ? return Promise.reject('登錄超時(shí),請(qǐng)重新登錄')
? ? ? }
? ? }
? ? if (response.config.handleResponse) {
? ? ? return response
? ? } else {
? ? ? const { head, body } = response.data
? ? ? // 舊數(shù)據(jù)格式分為head和body兩部分,現(xiàn)在使用ApiResponse不會(huì)再出現(xiàn)這兩部分,此處為兼容舊格式的臨時(shí)方案
? ? ? if (head && body) {
? ? ? ? // 正常返回
? ? ? ? if (+head.code === 1) {
? ? ? ? ? return body
? ? ? ? } else {
? ? ? ? ? return Promise.reject(head.msg || '未知錯(cuò)誤')
? ? ? ? }
? ? ? } else {
? ? ? ? const { status, statusText, data } = response.data
? ? ? ? if (+status === 0) {
? ? ? ? ? return data
? ? ? ? } else {
? ? ? ? ? if (response.config.handleError) {
? ? ? ? ? ? return Promise.reject(response.data)
? ? ? ? ? } else {
? ? ? ? ? ? return Promise.reject(statusText || '未知錯(cuò)誤')
? ? ? ? ? }
? ? ? ? }
? ? ? }
? ? }
? },
? error => {
? ? // 非取消請(qǐng)求
? ? if (!axios.isCancel(error)) {
? ? ? // 401無(wú)權(quán)限判斷
? ? ? if (error.response && error.response.status === 401) {
? ? ? ? return Promise.reject('沒(méi)有接口權(quán)限,請(qǐng)聯(lián)系管理員')
? ? ? } else if (error.response && error.response.status === 531) {
? ? ? ? setTimeout(() => {
? ? ? ? ? // 清除登錄狀態(tài)
? ? ? ? ? removeToken()
? ? ? ? ? window.location.href = '/'
? ? ? ? }, 1000)
? ? ? ? return Promise.reject('登錄超時(shí),請(qǐng)重新登錄')
? ? ? }
? ? }
? ? return Promise.reject(error)
? }
)
?
export default service

request.js中把token內(nèi)容抽離出來(lái)方便管理

src下新建/utils/auth.js

import Cookies from 'js-cookie'
?
const TokenKey = 'Admin-Token'
?
export function getToken() {
? return Cookies.get(TokenKey)
}
?
export function setToken(token) {
? if (!token || token === 'null') {
? ? return
? }
? sessionStorage.setItem('jwt', token)
? return Cookies.set(TokenKey, token)
}
?
export function removeToken() {
? return Cookies.remove(TokenKey)
}

最終頁(yè)面使用axios

import { getAppConfig } from '@/api/app'
//請(qǐng)求
?const params = {}
? ? ? ? getAppConfig(params).then(res => {
? ? ? ? ? this.loading = false
? ? ? ? ? console.log(res)
? ? ? ? }).catch(e => {
? ? ? ? ? this.loading = false
? ? ? ? ? this.$message.error(e.toString())
? ? ? ? })

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue生命周期activated之返回上一頁(yè)不重新請(qǐng)求數(shù)據(jù)操作

    Vue生命周期activated之返回上一頁(yè)不重新請(qǐng)求數(shù)據(jù)操作

    這篇文章主要介紹了Vue生命周期activated之返回上一頁(yè)不重新請(qǐng)求數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • vue.js $refs和$emit 父子組件交互的方法

    vue.js $refs和$emit 父子組件交互的方法

    本篇文章主要介紹了vue.js $refs和$emit 父子組件交互的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • vue中v-for加載本地靜態(tài)圖片方法

    vue中v-for加載本地靜態(tài)圖片方法

    下面小編就為大家分享一篇vue中v-for加載本地靜態(tài)圖片方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • vue路由守衛(wèi)+登錄態(tài)管理實(shí)例分析

    vue路由守衛(wèi)+登錄態(tài)管理實(shí)例分析

    這篇文章主要介紹了vue路由守衛(wèi)+登錄態(tài)管理,結(jié)合實(shí)例形式分析了vue路由守衛(wèi)與登錄態(tài)管理相關(guān)操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-05-05
  • vue實(shí)現(xiàn)購(gòu)物車(chē)完整功能

    vue實(shí)現(xiàn)購(gòu)物車(chē)完整功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)購(gòu)物車(chē)完整功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • vue 項(xiàng)目打包通過(guò)命令修改 vue-router 模式 修改 API 接口前綴

    vue 項(xiàng)目打包通過(guò)命令修改 vue-router 模式 修改 API 接口前綴

    這篇文章主要介紹了vue 項(xiàng)目打包通過(guò)命令修改 vue-router 模式 修改 API 接口前綴的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-06-06
  • vue?項(xiàng)目頁(yè)面卡死原因排查分析

    vue?項(xiàng)目頁(yè)面卡死原因排查分析

    這篇文章主要介紹了vue?項(xiàng)目頁(yè)面卡死原因排查分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue如何跨組件傳遞Slot的實(shí)現(xiàn)

    Vue如何跨組件傳遞Slot的實(shí)現(xiàn)

    這篇文章主要介紹了Vue如何跨組件傳遞Slot的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • vue-table實(shí)現(xiàn)添加和刪除

    vue-table實(shí)現(xiàn)添加和刪除

    這篇文章主要為大家詳細(xì)介紹了vue-table實(shí)現(xiàn)添加和刪除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Vue獲取input值的四種常用方法

    Vue獲取input值的四種常用方法

    Vue是一種流行的Web開(kāi)發(fā)框架,它提供了一個(gè)雙向綁定的語(yǔ)法糖。在Vue中,我們可以很容易地獲取頁(yè)面上的數(shù)據(jù),并且可以實(shí)時(shí)的響應(yīng)其變化,這篇文章主要給大家介紹了關(guān)于Vue獲取input值的四種常用方法,需要的朋友可以參考下
    2023-09-09

最新評(píng)論

明溪县| 岳普湖县| 寻乌县| 泰顺县| 大英县| 洛南县| 临西县| 苗栗县| 且末县| 云南省| 信宜市| 广德县| 南陵县| 额尔古纳市| 永济市| 汽车| 红河县| 信宜市| 肇州县| 东乡族自治县| 齐齐哈尔市| 尼勒克县| 昂仁县| 忻城县| 固镇县| 措勤县| 五原县| 广昌县| 禄丰县| 洪洞县| 临沭县| 洱源县| 乌鲁木齐市| 望城县| 姚安县| 印江| 嘉善县| 宁德市| 冷水江市| 台湾省| 沙河市|