>  기사  >  웹 프론트엔드  >  Vue 프로젝트에서 전역적으로 Axios를 사용하는 방법 소개

Vue 프로젝트에서 전역적으로 Axios를 사용하는 방법 소개

不言
不言앞으로
2019-03-20 11:44:352738검색

이 글은 Vue 프로젝트에서 전 세계적으로 Axios를 사용하는 방법을 소개합니다. 도움이 필요한 친구들이 참고할 수 있기를 바랍니다.

세 가지 방법이 있습니다.

1. vue-axios와 함께 사용하세요.

첫 번째 인용문은

import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios,axios);

this.axios.get('/api/usrmng')
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log(error);
});

에서 사용하실 수 있습니다.

import axios from 'axios'
Vue.prototype.$http = axios

2. axios는 Vue

메인 항목 파일 main.js에서 먼저 참조된 다음 vue의 프로토타입 체인에 걸려 있습니다.

this.$http.get('/api/usrmng')
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log(error);
});

컴포넌트에 사용됨

import Vue from 'Vue'
import Vuex from 'vuex'

import axios from 'axios'

Vue.use(Vuex)
const store = new Vuex.Store({
  // 定义状态
  state: {
    user: {
      name: 'root'
    }
  },
  actions: {
    // 封装一个 ajax 方法
    login (context) {
      axios({
        method: 'post',
        url: '/user',
        data: context.state.user
      })
    }
  }
})

export default store

3. Vuex의 action

과 결합하여 vuex Warehouse 파일 store.js에서 참조하고 action add 메소드

를 사용합니다.

methods: {
  submitForm () {
     this.$store.dispatch('login')
  }
}

컴포넌트에서 요청을 보낼 때 this.$store.dispatch

rrreee

을 사용하세요. 더 많은 흥미로운 콘텐츠를 보려면 🎜JavaScript 튜토리얼 동영상🎜을 참조하세요. PHP 중국어 웹사이트의 칼럼! 🎜

위 내용은 Vue 프로젝트에서 전역적으로 Axios를 사용하는 방법 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 cnblogs.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제