ホームページ  >  記事  >  ウェブフロントエンド  >  vue-cliの導入とaxiosの設定手順を詳しく解説

vue-cliの導入とaxiosの設定手順を詳しく解説

php中世界最好的语言
php中世界最好的语言オリジナル
2018-05-23 10:41:294262ブラウズ

今回は vue-cli の導入と axios の設定手順について詳しく説明します。 vue-cli の導入と axios の設定における 注意事項 は何ですか? 以下は実践的なケースですので、見てみましょう。

1. axios を npm でインストールします。手順は次のとおりです

npm install axios --save-dev

npm install axios --save-dev 

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

import axios from 'axios' 

接着将axios改写为Vue的原型属性,

Vue.prototype.$http=axios 

这样之后就可在每个组件的methods中调用$http命令完成数据请求

三、在组件中使用

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

有关axios的配置请参考如下文档,点击打开链接

下面给大家介绍下vue-cli配置axios的方法

1.

npm install axios --save2. main.js 内で

import axios from 'axios'

次に axios を Vue のプロトタイプ属性として書き換えます、

Vue.prototype.$http=axios

これにより、毎回使用することができます 各コンポーネントのメソッドで $http コマンドを呼び出してデータリクエストを完了します

3. コンポーネント内で

npm install @type/axios --save-dev(使用ts编写的需要此声明文件,升级的axios好像不需要了,已经自带)
を使用します

axios の設定については、次のドキュメントを参照してください。リンクを開きます

vue-cli で axios を設定する方法を紹介します

1.

npm install axios --save

2.

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: '权限无效',
   message: '您的用户信息已经失效, 请重新登录',
   type: 'warning',
   offset: 48
  });
  window.location.href = '/#/login'
 }else{
  Notification({
   title: '请求错误',
   message: `${error.response.status} \n ${error.config.url}`,
   type: 'error',
   offset: 48,
  })
 }
 return Promise.reject(error)
})
export default axios

3.

axios.ts ファイルを src ディレクトリに追加します。内容:

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

新しい vue.d.ts ファイルを作成します。タイプフォルダー、コンテンツ:

var path = require('path')
var rootpath = path.resolve(dirname, '../dist')
module.exports = rootpath
このようにして、axios を使用している各モジュールで this.$axios を使用できます
axios:🎜1 の Build.rootpath.js content:🎜rrreee🎜2。 Store は vuex ファイルであるため、事前に vuex をインストールする必要があります🎜🎜この記事の事例などを読んで、方法をマスターしたと思います。なんと興味深いことでしょう。php 中国語 Web サイトの他の関連記事にも注目してください。 🎜🎜推奨読書: 🎜🎜🎜JS の蓄積、反復、枯渇、再帰などの一般的なアルゴリズムの使用の概要🎜🎜🎜🎜🎜 React ファミリーのバケット環境を構築するためのコード分析🎜🎜🎜

以上がvue-cliの導入とaxiosの設定手順を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。