手把手式笔记

  1. 安装 axios
  1. npm install axios
  1. main.js同级目录新建axios配置文件setaxios.js
  1. import axios from 'axios'
  2. // import store from './store' //vuex
  3. // import router from './router' //路由
  4. export default function setAxios() {
  5. //拦截request请求
  6. axios.interceptors.request.use(
  7. config=>{
  8. console.log(config.data);
  9. return config;
  10. }
  11. )
  12. //拦截response回调
  13. axios.interceptors.response.use(
  14. response=>{
  15. if(response.status===200){
  16. const data=response.data
  17. // if (data.code === 400){
  18. // //登录过期,权限不足
  19. // console.warn("登陆过期");
  20. // //清除token
  21. // store.commit('setToken','')
  22. // window.localStorage.removeItem('token')
  23. // //跳转登录
  24. // router.replace({
  25. // path:"/login"
  26. // })
  27. // }
  28. return data;
  29. }
  30. return response;
  31. }
  32. )
  33. }
  1. main.js中引入axios与其配置文件
  1. import axios from 'axios'
  2. import setaxios from './setaxios'
  3. //Vue全局挂载axios
  4. Vue.prototype.$http=axios
  5. //设置baseUrl
  6. axios.defaults.baseURL = '/api'
  1. module.exports = {
  2. publicPath: './',
  3. outputDir: 'dist',
  4. assetsDir: 'static',
  5. configureWebpack: {
  6. devServer: {
  7. contentBase: './build',//项目基本访问目录
  8. host: 'localhost',//服务器ip地址
  9. port: 8088,//端口
  10. open: true, //自动打开页面
  11. hot: true,//模块热替换
  12. hotOnly: true,//只有热更新不会刷新页面
  13. //mock数据接口部分 关键部分
  14. before(app) {
  15. const bodyParser = require('body-parser')
  16. app.use(bodyParser.json()) //通过bodyParser获取req.body)
  17. /**
  18. * testGet
  19. */
  20. app.get('/api/test/get',(req,resp)=>{
  21. console.log(req.query);
  22. resp.json({
  23. "code":111,
  24. "msg":"get测试成功"
  25. })
  26. })
  27. /**
  28. * testPost
  29. */
  30. app.post('/api/test/post', (req, resp) => {
  31. console.log(req.body);
  32. resp.json({
  33. "code": 123,
  34. "msg": "post测试成功"
  35. })
  36. })
  37. /**
  38. * testPut
  39. */
  40. app.put('/api/test/put', (req, resp) => {
  41. console.log(req.body)
  42. resp.json({
  43. "code": 123,
  44. "msg": "put测试成功"
  45. })
  46. })
  47. /**
  48. * testDelete
  49. */
  50. app.delete("/api/test/delete",(req,resp)=>{
  51. console.log(req.body);
  52. resp.json({
  53. "code":666,
  54. "msg":"delete测试成功"
  55. })
  56. })
  57. }
  58. }
  59. }
  60. }

通过上述配置操作即可完成本地mock数据接口编写,接下来是axios发送http请求测试示例

  1. methods: {
  2. sendGet: function() {
  3. this.$http
  4. .get("/test/get", {
  5. params: {
  6. param1: "get字符串",
  7. param2: 13131
  8. }
  9. })
  10. .then(res => {
  11. console.log(res);
  12. });
  13. },
  14. sendPost: function() {
  15. this.$http
  16. .post("/test/post", {
  17. param1: "post字符串",
  18. param2: 13131
  19. })
  20. .then(res => {
  21. console.log(res);
  22. });
  23. },
  24. sendPut: function() {
  25. this.$http
  26. .put("/test/put", {
  27. param1: "put字符串",
  28. param2: 13131
  29. })
  30. .then(res => {
  31. console.log(res);
  32. })
  33. .catch(err => {
  34. console.log(err);
  35. });
  36. },
  37. sendDelete: function() {
  38. this.$http
  39. .delete("/test/delete", {
  40. data: {
  41. param1: "delete字符串",
  42. param2: 13131
  43. }
  44. })
  45. .then(res => {
  46. console.log(res);
  47. })
  48. .catch(err => {
  49. console.log(err);
  50. });
  51. }
  52. }
  1. <template>
  2. <div>
  3. <h2>HTTP-Request</h2>
  4. <button @click="sendGet()">GET</button>
  5. <span>&emsp;&emsp;</span>
  6. <button @click="sendPost()">POST</button>
  7. <span>&emsp;&emsp;</span>
  8. <button @click="sendPut()">PUT</button>
  9. <span>&emsp;&emsp;</span>
  10. <button @click="sendDelete()">DELETE</button>
  11. <hr />
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. name: "testPage",
  17. data() {
  18. return {};
  19. },
  20. methods: {
  21. sendGet: function() {
  22. this.$http
  23. .get("/test/get", {
  24. params: {
  25. param1: "get字符串",
  26. param2: 13131
  27. }
  28. })
  29. .then(res => {
  30. console.log(res);
  31. });
  32. },
  33. sendPost: function() {
  34. this.$http
  35. .post("/test/post", {
  36. param1: "post字符串",
  37. param2: 13131
  38. })
  39. .then(res => {
  40. console.log(res);
  41. });
  42. },
  43. sendPut: function() {
  44. this.$http
  45. .put("/test/put", {
  46. param1: "put字符串",
  47. param2: 13131
  48. })
  49. .then(res => {
  50. console.log(res);
  51. })
  52. .catch(err => {
  53. console.log(err);
  54. });
  55. },
  56. sendDelete: function() {
  57. this.$http
  58. .delete("/test/delete", {
  59. data: {
  60. param1: "delete字符串",
  61. param2: 13131
  62. }
  63. })
  64. .then(res => {
  65. console.log(res);
  66. })
  67. .catch(err => {
  68. console.log(err);
  69. });
  70. }
  71. }
  72. };
  73. </script>


webpack中文文档

Axios中文文档

如有不妥,不解之处,请滴滴我,或在评论区留言

版权声明:本文为roseAT原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/roseAT/p/11228369.html