Vue中封装axios的GET和POST请求
2020-06-28 19:18
381 查看
在vue项目中创建一个文件夹network
1.创建一个core.js文件,封装axios GET POST请求
代码如下:
import {GET,POST} from "./config" import axios from "axios"; const instance = axios.create({ baseURL:"https://api.it120.cc",//发送请求时,会在url前拼接baseUrl timeout:8000 }) //封装GET POST export function request(method,url,params){ switch (method){ case GET: return get(url,params) case POST: return post(url,params) } } function get(url,params){ return instance.get(url,params) } function post(url,params){ return instance.post(url,params) }
2.封装请求APl,处理接口数据
export const GET = "get"; export const POST ="post"; export const path ={ list:"/small4/shop/goods/list", }
3.抛出network模块
import {request} from "./core" import {GET,path} from "./config" const network = { getStoreList(params) {return request(GET,path.list,params)}, } export default network;
4main.js中配置
import network from "./network/index"; Vue.prototype.$network = network;
5.在home中使用
<template> <div class="home"> <button @click="onClick">点击</button> <div v-for="(item,index) in list" :key="index"> {{item.name}} <img :src="item.pic" alt="" width="200px"> </div> </div> </template> <script> // @ is an alias to /src export default { name: 'Home', data(){ return{ list:[], } }, components: { }, methods:{ onClick(){ this.$router.push(this.$routerConfig.about.path) } }, //调用 mounted(){ this.$network.getStoreList({ page:"1", pageSize:"10" }).then((response)=>{ console.log(response.data.data) this.list=response.data.data }).catch((error)=>{ console.log(error) }) } } </script>
相关文章推荐
- vue-cli的axios使用方法,get、post请求的跨域问题解决
- vue-cli封装get put delete post 请求(参数中带header)
- Vue使用axios发送get和post请求
- vue-axios封装get方法和post方法
- 使用vue的axios组件,执行post和get请求时,把token参数放入header,被nginx拦截,导致请求失败。
- vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete)...
- Vue的axios插件实现GET、POST、JSONP请求,以及请求拦截
- Vue简单封装axios之解决post请求后端接收不到参数问题
- vue使用axios发送get、post请求
- Vue.js Ajax(axios) GET和post请求
- Vue的axios插件实现GET、POST、JSONP请求,以及请求拦截
- vue引入axios发送get或post请求的方法
- axios中get、post请求的封装
- Vue------第五天(Vue的Ajax请求,使用Axios,目前熟悉了一下,主要包括GET请求、POST请求、并发请求、请求参数的配置、服务器响应的结构以及对服务器响应错误的简单处理)
- Vue axios post请求带参请求 详细教程
- vue后台管理:增加vuex状态管理和封装axios请求
- vue 封装 axios 网络请求
- vue axios POST请求中参数以form data和request payload形式的原因
- vue-08-axios-get-post-跨域
- Android 网络请求框架,包涵get,post请求,from表单上传图片,基于okhtt封装的图片上传