Axios Getting Started notes

Source: Internet
Author: User

Today on GitHub to see other people's projects, just recently planned to learn Axios, also is a collection of Vue family barrels, perhaps can summon the dragon.

First use Axios, first introduced: NPM install Axios and then in the place you want to use the direct import Axios from ' Axios ' can write the corresponding code

You can create an API folder directly under SRC to hold the Axios file (Api.js and Index.js), just like this:

The encapsulation request is defined in the Api.js file:

Import Axios from 'Axios'; LetBase="'; exportConstRequestlogin =params= = {returnAxios.post (' ${Base}/login ',params). Then (res =res.data); };exportConstGetuserlist =params= = {returnAxios.Get(`${Base}/user/list ', {params:params }); };exportConstGetuserlistpage =params= = {returnAxios.Get(`${Base}/user/listpage ', {params:params }); };exportConstRemoveuser =params= = {returnAxios.Get(`${Base}/user/remove ', {params:params }); };exportConstBatchremoveuser =params= = {returnAxios.Get(`${Base}/user/batchremove ', {params:params }); };exportConstEdituser =params= = {returnAxios.Get(`${Base}/user/edit ', {params:params }); };exportConstAddUser =params= = {returnAxios.Get(`${Base}/user/add ', {params:params}); };

Index.js is the entry file:

Import * as API from './api 'default API;

Let's talk about the POST request operation.

Take example code examples:

handleSubmit2 (EV) {var_this = This;  This. $refs. Ruleform2.validate ((valid) = {          if(valid) {//If you enter an account password that has a value                         This. logining =true;//Show loading icon            //Nprogress.start ();            varLoginparams = {username: This. Ruleform2.account, Password: This. Ruleform2.checkpass}; Let base= ' ';axios.post (' ${base}/login ', loginparams). Then (res = = {Console.log (res); return res.data}). Then (data = {               This. logining =false; //Nprogress.done ();Let {msg, code, user} =data; if(Code!== 200) {                 This. $message ({message:msg, type:' Error '                }); } Else{Sessionstorage.setitem (' User ', json.stringify (user));  This. $router. Push ({path: '/table ') });          }            }); } Else{Console.log (' Error submit!! '); return false;      }        }); }

This is a function of login detection, which mainly says this part:

Axios.post (' ${base}/login ', loginparams). Then (res + {return res.data}). Then (data = {this.logining = false;  If (Data.code!==) {                this. $message ({                  message:data.msg,                  ' error ')                 });               Else {                Sessionstorage.setitem (' user ', json.stringify (Data.user));  Here is the user information stored in the Sessionstorage, this is must have!                 this. $router. Push ({path: '/table ' });  Here is the path that the configuration request is completed or jumps to after the login is completed              }            });

Here Axios first Post sends the request, the request sends out will get a response, is the callback function parameter Res,consolelog found this res is a JSON, contains the following content:

Generally useful is the data object, so the first callback function returns RES.DATA, the second callback function will use the returned data object in the various properties to determine the response situation, including commonly used MSG, code, user and other information.

Summarize:

Login Login Verification using Axios: 1. Send Axios.post (URL, {params}). Then (response =>{return Res.data}). Then (data = {...} obtained RESPO The data object under NSE

2. Determine if the login request is correct based on the data object obtained

3. If the request is complete, that is data.code==200, you need to write the user information (Data.user) in the sessionstroage and configure the jump path

Just a very basic axios introduction, if there are errors, please correct me!

Axios Getting Started notes

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.