Pits encountered when using Axios in VUE-CLI scaffolding

Source: Internet
Author: User

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.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.