Vue项目中简易演示axios解耦

-api\sug.js  (配置获取方法)

-utils\request.js  (配置自定义axios实例)

-vue.config.js  (解决跨域)

-demo.vue  (引入并调用请求方法api\sug.js__function getSug())

步骤1:

module.exports = {
outputDir: 'dist', //build输出目录
assetsDir: 'assets', //静态资源目录(js, css, img)
lintOnSave: true, //是否开启eslint
devServer: {
open: false, //是否自动弹出浏览器页面
host: "localhost",
port: '8080',
https: false, //是否使用https协议
hotOnly: false, //是否开启热更新
proxy: {
'/api': {
target: 'https://suggest.taobao.com', //API服务器的地址
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}

vue.config.js 

步骤2:

import axios from 'axios'

const service = axios.create({
baseURL: '/api',
timeout: 1000
}) export default service

utils\request.js

步骤3

import request from '@/utils/request'

export function getSug(params) {
return request({
url: "/sug",
method: 'get',
params
})
}

api\sug.js

步骤4

<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
</div>
</template> <script>
import HelloWorld from './components/HelloWorld.vue'
import request from './utils/request.js'
import { getSug } from './api/sug.js'
export default {
name: 'app',
components: {
HelloWorld
},
mounted(){
getSug({
code:"utf-8",
q:"卫衣"
})
.then(res=>{
console.log(res)
})
.catch(err=>{
console.log(err)
})
}
};
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

App.vue

查看network

恭喜成功地把axios分离了

最新文章

  1. OpenCV2计算机编程手册(一)操作像素
  2. 高性能网站架构设计之缓存篇(5)- Redis 集群(上)
  3. C语言 指针例解
  4. linux文本模式下使用PPPOE拨号ADSL上网的方法
  5. DSP using MATLAB 示例Example3.4
  6. 【BZOJ】3196: Tyvj 1730 二逼平衡树(区间第k小+树套树)
  7. appium for windows 环境搭建
  8. Ajax Array Json 示例
  9. Go语言之defer
  10. android 焦点问题
  11. xmlplus 组件设计系列之十 - 网格(DataGrid)
  12. Redis学习-SortedSet
  13. poi合并单元格同时导出excel
  14. 判断是否AVL平衡二叉书
  15. 05-Git
  16. 软件测试实验四----mujava变异测试
  17. saltstack实战1-salt-syndic
  18. Android View相关知识问答
  19. ipconfig命令查ip的时候给别人看有危险吗
  20. mysql更改数据文件目录及my.ini位置

热门文章

  1. Python图像处理丨详解图像去雾处理方法
  2. Hadoop详解(01)-概论
  3. Atcoder Beginner Contest ABC 284 Ex Count Unlabeled Graphs 题解 (Polya定理)
  4. 使用C语言编程的7个步骤
  5. Lyndon Word 与 Lydon 分解
  6. win32com操作word API精讲&amp;项目实战 预告
  7. Docker 基础 - 2
  8. 【ASP.NET Core】动态映射MVC路由
  9. 聊一聊js中元素定位的方法
  10. 从历代GC算法角度刨析ZGC