TA的每日心情 | 奋斗 2020-9-2 15:06 |
|---|
签到天数: 2 天 [LV.1]初来乍到
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
第一步:" V: V. s) |0 V e
5 ~& T& S' W0 ?首先引入axios& I: E( ~8 @$ g1 {2 _
2 h+ a; ^5 |% R/ y# a
然后创建两个文件夹api和http
1 l, O5 @5 }6 b: K% Q, M
n' @6 f! O1 c5 \+ Khttp.js 里面的
% A6 b9 W! o6 W! v* f' U! S: Z$ l- Q2 b6 _2 I% C
复制代码
, v% ]7 }) ~' p5 J1 d1 import axios from ‘axios’;//引入axios
% T& l8 s: } ~ p. x# q2% t6 c( A- K/ Y- g
3 //环境的切换 开发环境(development)使用的是测试接口 和 生产环境(production)使用的是上线接口# Q* F! R' v8 B" r
4 if(process.env.NODE_ENV==‘development’){( F9 U- X l8 v9 W! H
5 //设置默认路径7 I+ |7 X. f T: t$ ~, ~: G1 q
6 axios.defaults.baseURL=‘http://120.53.31.103:84/’
2 i2 {( g( C1 B! f: `% k& C7 }
% x6 u. _; ?3 A5 a. [ J8 if(process.env.NODE_ENV==‘production’){
; o: D0 a M$ |4 n0 [' o8 S9 axios.defaults.baseURL=‘https://wap.365msmk.com/’9 u2 P5 D, R3 ~7 @: B0 s# H
10 }
+ j2 o% I/ y) t, z' U5 {7 F3 _* W( J11 axios.defaults.timeout=5000;//加载不出来5秒之后就是加载失败
$ F# ~" C- L. N12 axios.interceptors.request.use(
8 p5 v: k4 @5 S/ k. y v5 H13 config=>{9 v2 r2 J. U5 E
14 config.headers={DeviceType:‘H5’}//可每次发送请求之前的逻辑处理 比如判断token; B7 Z+ ^9 ]0 @* j8 a
15 return config;
( {1 [3 r7 c; i }+ T# J+ s5 P16 }
0 p# w2 h* H* }4 ^9 z7 g) K3 m& P17 )
|" z9 U B1 T: V8 @! p18 // axios.interceptors.response.use(" l6 ?. N. |7 V& I
19 // response=>{
6 H# W+ Y! C6 ?6 o20 // return response;1 P! {% q0 ]" C( W" }& \5 l
21 // },
! Z3 a. L+ K0 S0 x" \, p% t, C22 // error=>{
) i Y1 |, X' V% ]2 ]23 // if(error.response.status){
" G" x" n2 m( z+ G- A24
& W2 x9 k( b$ T4 y, m' k7 \2 P25 // }
7 V" T& n/ q4 p- a; A. L26 // }! L8 P$ Y* n9 f8 H7 n+ d
27 // )
$ c6 B- z4 w* l2 I* e& ?28
, F" [& n- h: W \$ U29 // 使用promise返回axios请求的结果+ W, g! V& _2 V% M- ~# q' k- b
30 export function get(url, params) { W/ N) O5 a4 ?+ b5 o# V' Y* w, C
31 return new Promise((resolve, reject) => {+ K3 N; \7 |! }; R- F, E+ _
32 axios.get(url, {5 V5 f. o* `2 A8 H/ I G
33 params:params$ a5 P1 u1 B, W
34 }).then(res => {( f, R* v0 G9 X& l8 M
35 resolve(res)6 u6 c7 ^) s$ a$ t
36 }).catch(err => {
) D: @; Z+ q8 p2 A! w37 reject(err)
6 r# _* X/ a. _# F+ _) O38 }). O( q0 b! p$ u2 Y9 B
39 })
) c5 x. }" l% m40 };6 ~7 _3 X$ p0 g6 C/ E0 j
41
8 X$ y3 A: ~$ R42 export function post(url,params){
0 i, k, ? I( E$ K2 b43 return new Promise((resolve,reject)=>{& x- H5 w1 m3 N# S
44 axios.post(url,params).then(res=>{
, Z# C, R1 [0 a; Z45 resolve(res.data)
7 S* f r0 a5 Y) n/ D8 E+ J! u46 }).catch(err=>{
7 d" x) `( I5 {2 _' n47 reject(err.data)
9 G; {1 q2 q2 Q48 })
! Z- e" R5 r" c7 i) w! i' A) M n49. d2 o5 i$ I J
50 })# W$ x3 G5 i. |
51 }
; i. n& y2 T( ^3 d; I6 w6 m3 |/ X复制代码
+ s% o1 o$ \: M9 d! d. P3 xapi.js里面的
0 g |2 m7 [4 P$ g
7 r' ], D+ E) u4 |7 D; j2 l复制代码
# ^9 A, p d/ v& Q* u7 n0 t# u6 Fimport { get, post } from “…/http/http” //引入封装好的get和post方法# Q2 { t( w$ L% K; p6 ~( K
// 封装请求的方式
* \' p& p$ Z1 F6 F; Nexport function getBanners() {//轮播
+ a, s% X1 Q- n9 E$ q2 j. Dreturn get(‘api/app/banner’)" h# L! s: u2 L- U# Q5 T7 N; ]3 ?
}5 {& Z5 ^& P& n
3 ]* M4 c8 O. L5 _3 |! k
export function getIndex(){//首页数据
: U& f$ a: m- _: Q' Yreturn get(‘api/app/recommend/appIndex’)
: ^ m4 q" c! O$ I) C1 I' ]# d# ?}# C/ ? Y9 o' f
复制代码
. |8 x9 J: n) P1 I2 H0 O$ n然后在vue里面的文件应用
4 S# n: q' u' t- W, ]' B' {( E- [, @$ u( E
复制代码 G1 y7 A. T9 S
import { getBanners, getIndex } from “…/api/api”;//引入api里面定义的方法, L0 P6 Q0 ~) I6 w. k; H4 k
p+ R( u5 w3 H/ Nasync mounted() {! @6 Q4 Q- d4 h' [$ Q4 t
var swiperr = await getBanners();
% e2 q4 v, H1 s- w4 Tconsole.log(swiperr.data.data);
1 N7 E- X2 W8 ^2 l, L$ {this.swipers = swiperr.data.data; //轮播图渲染' k; R8 I; [3 w2 u* e; P3 q
7 ^, z6 f, N, Uvar strr = await getIndex();7 ^3 S7 i% O# y) P8 G5 J/ ]9 `* L
console.log(strr); //明星讲师渲染 G4 U2 Y8 K& w& M
this.strs = strr.data.data;
7 D" V+ f7 Z* x& a% [2 bconsole.log(this.strs);
; o0 {$ {8 \% }5 k: J1
/ {* c" [" {( V( _+ I2& n$ X4 J6 Z2 d! ^# `( ?
3
' b5 H) [: {7 O4
+ `- c0 P% h: `- o% A}
0 h" d/ F" k' V3 r
% y+ e2 K6 P, i. i/ `9 E |
|