Use axios to get http request data in VUE

First use npm install axios
and then register the methods used by the global environment in main.js, such as:

import axios from 'axios'
//这时候如果在其它的组件中,是无法使用 axios 命令的。但如果将 axios 改写为 Vue 的原型属性,就能解决这个问题
Vue.prototype.$ajax = axios

After adding these two lines of code in main.js, you can use the $ajax command directly in the methods of the component

this.$ajax.get('data.json').then(function (res) {
    this.data = res.data
}.bind(this))

These two callback functions have their own independent scopes. If you access this directly in it, you cannot access the Vue instance.
Then you only need to add a .bind(this) to solve this problem.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324725809&siteId=291194637