Use Axios Element to implement the global request loading method, axiosloading
Background
This is the business requirement. Each time a request is sent to the backend, a full-screen loading is triggered, and multiple requests are merged into one loading.
Currently, vue, axios, and element are used in the project. This article mainly describes how to use axios and
Axios post submits an instance of formdata, axiosformdata
Axios is recommended for the vue framework to send ajax requests.A blogTo explain how to use axios In the vue component. However, the get requests were used for previous purposes. Now I used the post method when I set up my own blog. I found that the backend (node. js) could not get any parameters from the
After Vue has been updated to 2.0, the author declares that it is no longer a vue-resource update, but rather the recommended Axios, which has been used for a while, and now for its basic usage.The first is to introduce Axios, if you use ES6, just install the Axios moduleImport Axios from '
There are roughly three problems with Vue interacting with Axios:1: Debugging cross-domain issues locally?2:post request, the argument failed, causing the request to fail?3:axios reference, referenced in the component usedSolution:Question one: cross-domain?Troubleshoot local debugging cross-domain issues?Reverse proxy----This needs to be in the configuration file to change the configuration, there is a con
Recently when using the Vue refactoring Company M station, Axios was used to request data, and as a result of the project, the Axios was encapsulated two times, click into Axios
//引入axiosimport axios from ‘axios‘let cancel ,promiseArr = {}const CancelToken = axios.CancelToken;//请求拦截器axios.interceptors.requ
automatically determine whether the current development or production environment, and then automatically match the Api_host, we can use the Process.env.API_HOST in any component of the address such asInstance.post (process.env.api_host+'user/login'this. form)Then the second step back-end server configuration Cros cross-domain, that is, access-control-allow-origin:* allows all access to the meaning.Comprehensive: Under the environment of development we can configure a proxy agent can cross the
Analysis on axios's inability to use Vue. use (), axiosvue. use
Preface
I have encountered some problems in using axios recently. I feel it is necessary to share with you the basic knowledge about axios. For details, refer to this article: how to get started with axios, I won't talk much about it below. Let's take a look at the detailed introduction.
Problem
I be
Axios is the official recommended HTTP request tool for vue2.0, before the Vue-resourceTwo ways of using it are summarized in the process:1. Use similar with Vue-resourceIntroduced:import axios from ‘axios‘; Vue.prototype.$http = axios;Use:this.$http.get(URL).then(response => { // success callback }, response =>
First, installation1. Use NPM to install NPM installed Axios--save2, using Bower installation bower install Axios--save3. Direct use of CDN to introduce (direct ignore of VUE framework)Bind (this) a lot of people like to define a that in the outside, which is a reason to point to the external this. In order to resolve the inability to access the Vue instance. Catch error HandlingAxios cannot be used, it ca
Kapture 2018-06-07 at 14.57.40.gifdemo in GitHub backgroundBusiness requirements are such that a full-screen loading is triggered whenever a request is sent to the backend, and multiple requests are merged into a single loading.Now the project is using Vue, Axios, element, etc., so the article is mainly about the use of Axios and element to achieve this function.AnalysisFirst, the request begins with a load
0--Project DescriptionNote that this project originates from a course network shopping cart tutorial, but in the development process, found in the development process or vue-resource (announced the non-updated class $.AJX () plug-in), for future development use Axios.js, details reference1 Initial static pagedelete Cancel Select allTotal:2 Cart.js/** * Created by Administrator on 2017/4/2 0002.*/Let Vmcart=NewVue ({el:' #app ', data: {productlist: []//as a product list}, filter: {//Local Filter
First, IntroductionLook at the profile of the official website:"Promise based HTTP client for the browser and node. js"HTTP request client based on Promise, which can be used in both the browser and node. js.Second, features:1, send xmlhttprequests request in the browser;2. Send HTTP requests in node. js;3, support Promise API;4, interception request and response;5, conversion request and response data;6. Automatic conversion of JSON data;7, the client supports the protection of security from XS
.
See the complete approach/src/router.js
Among them, to.meta we customize the data, including the fields we just defined requireAuth . Use this field to determine whether the route requires logon permissions. If required, the current app does not have tokens, then jump to the login page and sign in. Jump to destination route after successful login.Login intercept is this the end of the block? And not. This approach is simply a front-end routing control and does not really prevent
Refer to the code of this encapsulation API to understand the problem with basePlus an understanding of ' ${base/login} ' below encapsulates Axios request for invocation Let base = "Export Const Requestlogin = params + = {return Axios.post (' ${base}/login ', params). Then (res = res.data)}Export Const Reqsaveuserprofile = params + = {return Axios.post (' ${base}/user/profile ', params). Then (res = Res.dat A)}Export Const Reqgetuserlist = params +
invalid data, key is {"FirstName": "Fred", "LastName": "Flintstone"} , value is empty. To use the application/x-www-form-urlencoded format, data conversion is required, although there are two ways URLSearchParams and qs two ways. I prefer qs库 the way to use the code as follows:axios.interceptors.request.use((req) => { if (req.method === ‘post‘) { req.data = qs.stringify(req.data); } return req;}, (error) => Promise.reject(error));When you use
Axios defaults to a method that does not have a JSONP cross-domain request. It is generally a popular practice to put the cross-domain in the background to resolve, that is, the background developers add cross-domain header information.For example, in JavaHeader,response.setheader ("Access-control-allow-origin", "www.allow-domain.com")However, many times, the background for some reasons do not want to modify or have written JSONP interface needs to ad
Axios defaults to a method that does not have a JSONP cross-domain request. It is generally a popular practice to put the cross-domain in the background to resolve, that is, the background developers add cross-domain header information.For example, in JavaHeader,response.setheader ("Access-control-allow-origin", "www.allow-domain.com")However, many times, the background for some reasons do not want to modify or have written JSONP interface needs to ad
In the Vue project, the list data needs to use the QQ music interface data, but the direct request does not have the host and the referer limit, needs to adopt the way of the back-end proxy. With Axios and Node Express, after implementation in Dev-server.js, restart the project, resulting in interface 404. The code is as follows: After careful examination, the code is no problem, so in apirouts Console.log () print information, run no printing inform
Axios with the traditional function method, this is not an instance of Vue. To change the data in the Vue, you must use the arrow method. (This in the Arrow method refers to the up-level this)Let vm= thenew Vue ({ el:' #app ', created () {this. InitData (); } , methods: { InitData () { axios.get (' Db.json '). Then (theres=> {this . products=Res.data;} , e
Recently use react to do project practiced hand, the project call interface has cross-domain problem, the reference plug-in is Axios,react project is created with Create-react-app, solve cross-domain problems can be set in the background Cros (cross-domain resource sharing), if the front-end solution can use the proxy, Projects built with Webpack can be configured in Webpack.config.js, Create-react-app-created projects Webpack configuration integratio
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.