亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue中使用axios固定url請求前綴

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

使用axios固定url請求前綴

main.js中添加:

使用方法:

定義axios默認路徑前綴或動態(tài)修改前綴

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

.env.development 開發(fā)文件中寫入要用的服務編碼

# 微服務編碼
VUE_APP_SERVICE_PREFIX = '/0201010254'

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

module.exports = {
? /**
? ?* 主站標題
? ?* @type {string}
? ?*/
? title: '開發(fā)項目名稱',
?
? /**
? ?* @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,
? /**
? ?* 服務編碼
? ?*/
? servicePrefix: process.env.VUE_APP_SERVICE_PREFIX,
?
}

之后請求文件中引入

新建api/app.js 封裝axios請求并引用自定義的服務編碼文件

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

其中的request文件是引用自建的請求攔截文件,根據(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
? ? // 設置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('沒有接口權限,請聯(lián)系管理員')
? ? ? } else {
? ? ? ? setTimeout(() => {
? ? ? ? ? // 清除登錄狀態(tài)
? ? ? ? ? removeToken()
? ? ? ? ? window.location.href = '/'
? ? ? ? }, 1000)
? ? ? ? return Promise.reject('登錄超時,請重新登錄')
? ? ? }
? ? }
? ? if (response.config.handleResponse) {
? ? ? return response
? ? } else {
? ? ? const { head, body } = response.data
? ? ? // 舊數(shù)據(jù)格式分為head和body兩部分,現(xiàn)在使用ApiResponse不會再出現(xiàn)這兩部分,此處為兼容舊格式的臨時方案
? ? ? if (head && body) {
? ? ? ? // 正常返回
? ? ? ? if (+head.code === 1) {
? ? ? ? ? return body
? ? ? ? } else {
? ? ? ? ? return Promise.reject(head.msg || '未知錯誤')
? ? ? ? }
? ? ? } 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 || '未知錯誤')
? ? ? ? ? }
? ? ? ? }
? ? ? }
? ? }
? },
? error => {
? ? // 非取消請求
? ? if (!axios.isCancel(error)) {
? ? ? // 401無權限判斷
? ? ? if (error.response && error.response.status === 401) {
? ? ? ? return Promise.reject('沒有接口權限,請聯(lián)系管理員')
? ? ? } else if (error.response && error.response.status === 531) {
? ? ? ? setTimeout(() => {
? ? ? ? ? // 清除登錄狀態(tài)
? ? ? ? ? removeToken()
? ? ? ? ? window.location.href = '/'
? ? ? ? }, 1000)
? ? ? ? return Promise.reject('登錄超時,請重新登錄')
? ? ? }
? ? }
? ? return Promise.reject(error)
? }
)
?
export default service

request.js中把token內容抽離出來方便管理

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

最終頁面使用axios

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

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • vue自定義組件search-box示例詳解

    vue自定義組件search-box示例詳解

    這篇文章主要介紹了vue自定義組件search-box,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • 在vue中實現(xiàn)禁止屏幕滾動,禁止屏幕滑動

    在vue中實現(xiàn)禁止屏幕滾動,禁止屏幕滑動

    這篇文章主要介紹了在vue中實現(xiàn)禁止屏幕滾動,禁止屏幕滑動,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • ant-design-vue按鈕樣式擴展方法詳解

    ant-design-vue按鈕樣式擴展方法詳解

    這篇文章主要為大家介紹了ant-design-vue按鈕樣式擴展方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • 深入解析vue中的權限管理

    深入解析vue中的權限管理

    權限是對特定資源的訪問許可,所謂權限控制,也就是確保用戶只能訪問到被分配的資源,這篇文章主要介紹了vue的權限管理的相關知識,需要的朋友可以參考下
    2022-06-06
  • vue.js select下拉框綁定和取值方法

    vue.js select下拉框綁定和取值方法

    下面小編就為大家分享一篇vue.js select下拉框綁定和取值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Element ui table表格內容超出隱藏顯示省略號問題

    Element ui table表格內容超出隱藏顯示省略號問題

    這篇文章主要介紹了Element ui table表格內容超出隱藏顯示省略號問題,具有很好的參考價值,希望對大家有所幫助,
    2023-11-11
  • vue使用prop可以渲染但是打印臺報錯的解決方式

    vue使用prop可以渲染但是打印臺報錯的解決方式

    今天小編就為大家分享一篇vue使用prop可以渲染但是打印臺報錯的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue項目實戰(zhàn)之圓柱狀水波效果實現(xiàn)

    vue項目實戰(zhàn)之圓柱狀水波效果實現(xiàn)

    最近工作中實現(xiàn)的一個效果不錯,分享給大家,下面這篇文章主要給大家介紹了關于vue項目實戰(zhàn)之圓柱狀水波效果實現(xiàn)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • Vue提供的三種調試方式你知道嗎

    Vue提供的三種調試方式你知道嗎

    這篇文章主要為大家介紹了Vue提供的三種調試方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • vue3.0 CLI - 2.3 - 組件 home.vue 中學習指令和綁定

    vue3.0 CLI - 2.3 - 組件 home.vue 中學習指令和綁定

    這篇文章主要介紹了vue3.0 CLI - 2.3 - 組件 home.vue 中學習指令和綁定的相關知識,本文通過實例代碼相結合的形式給大家介紹的非常詳細 ,需要的朋友可以參考下
    2018-09-09

最新評論