Previously used is Vue-resource, but after vue2.0, especially said that the official did not recommend Vue-resource, so began to learn to use Axios.
Install Axios:
NPM Install Axios
It looks like it's normal. NPM installation dependencies, Axios is also recommended by the official installation. But then the pit came out.
The error ...
Is not a face Meng, the official is written like this, you tell me wrong.
Well, yes, after trying countless times, I've found that it's no problem to install Axios in a project, only if it's in my project. And my project is built by VUE-CLI. So it is not possible to install Axios in the scaffold.
Then how to solve it.
CNPM Install Axios
Yes, you are not mistaken, NPM is not good, but CNPM is OK.
CNPM believe everyone is very familiar with, is Taobao to NPM's mirror file.
Installation method:
$ NPM install-g cnpm--registry=https://registry.npm.taobao.org
That's fine. Finally can use Axios in the project.
The following is how Axios is used:
1. Introduction of the Package
Import Axios from ' Axios '
2. Because Axios is not a Vue plug-in, Vue.use () cannot be used. Therefore, it is introduced by means of controlling the prototype chain.
Vue.prototype. $http = Axios
In this way, you can use the Axios method in your project.
this. $http. Get (' Https://yesno.wtf/api '). Then (function (response) {
console.log (response.data)
})
. catch (function (error) {
Console.log (Error)
})
There is also a detailed usage that can be seen in the official document.