成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

vue-cli引入、配置axios的方法

一、npm 安裝axios,文件根目錄下安裝,指令如下

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、梅縣網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為梅縣等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

npm install axios --save-dev 

二、修改原型鏈,在main.js中引入axios

import axios from 'axios' 

接著將axios改寫為Vue的原型屬性,

Vue.prototype.$http=axios 

這樣之后就可在每個(gè)組件的methods中調(diào)用$http命令完成數(shù)據(jù)請求

三、在組件中使用

methods: { 
   get(){ 
    this.$http({ 
     method:'get', 
     url:'/url', 
     data:{} 
    }).then(function(res){ 
     console.log(res) 
    }).catch(function(err){ 
     console.log(err) 
    }) 
     
    this.$http.get('/url').then(function(res){ 
     console.log(res) 
    }).catch(function(err){ 
     console.log(err) 
    }) 
   }    
}        

有關(guān)axios的配置請參考如下文檔,點(diǎn)擊打開鏈接

下面給大家介紹下vue-cli配置axios的方法

1.

npm install axios --save

2.

npm install @type/axios --save-dev(使用ts編寫的需要此聲明文件,升級的axios好像不需要了,已經(jīng)自帶)

3.

在src目錄下添加axios.ts文件,內(nèi)容:

import axios from 'axios'
import {Notification} from 'element-ui'
import store from './store/index'
import buildconf from '../config/build.rootpath.js'
axios.defaults.withCredentials = true;
axios.defaults.baseURL = buildconf.serverUrl
// axios.defaults.baseURL = 'http://gsblackwidow.chinacloudsites.cn/'
axios.interceptors.request.use(function(config) {
 // document.getElementById('g-loader').style.display = 'flex'
 store.commit('requestModify', 1)
 return config;
}, function(error){
 return Promise.reject(error)
})
axios.interceptors.response.use(function(response){
 store.commit('requestModify', -1)
 // document.getElementById('g-loader').style.display = 'none' 
 return response.data;
}, function(error){
 store.commit('requestModify', -1) 
 // document.getElementById('g-loader').style.display = 'none'  
 if(error.response.status === 401){
  Notification({
   title: '權(quán)限無效',
   message: '您的用戶信息已經(jīng)失效, 請重新登錄',
   type: 'warning',
   offset: 48
  });
  window.location.href = '/#/login'
 }else{
  Notification({
   title: '請求錯(cuò)誤',
   message: `${error.response.status} \n ${error.config.url}`,
   type: 'error',
   offset: 48,
  })
 }
 return Promise.reject(error)
})
export default axios

4.

types文件夾中新建vue.d.ts文件,內(nèi)容:

import {AxiosStatic, AxiosInstance } from 'axios'
declare module 'vue/types/vue' {
 interface Vue {
  $axios: AxiosStatic;
 }
}

這樣就可以在各個(gè)模塊中通過this.$axios來使用axios了

其中axios中:

1. build.rootpath.js內(nèi)容:

var path = require('path')
var rootpath = path.resolve(__dirname, '../dist')
module.exports = rootpath

2. store是vuex的文件,所以要事先安裝vuex

總結(jié)

以上所述是小編給大家介紹的vue-cli 引入、配置axios的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

文章標(biāo)題:vue-cli引入、配置axios的方法
文章源于:http://jinyejixie.com/article36/pshopg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、全網(wǎng)營銷推廣外貿(mào)網(wǎng)站建設(shè)、面包屑導(dǎo)航企業(yè)網(wǎng)站制作、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
秀山| 丹凤县| 绥棱县| 同仁县| 自贡市| 资阳市| 自贡市| 北流市| 五莲县| 山东省| 广东省| 鹤庆县| 延津县| 武强县| 霞浦县| 格尔木市| 江阴市| 宣汉县| 霍山县| 吐鲁番市| 泰州市| 浠水县| 陕西省| 镇赉县| 怀集县| 芦山县| 通渭县| 工布江达县| 信阳市| 广水市| 舒兰市| 江山市| 张家界市| 五指山市| 波密县| 安仁县| 新津县| 宜黄县| 浏阳市| 无为县| 陇南市|