Vue脚手架配置代理、安装axios

曾经终败给现在 2024-04-06 09:18 210阅读 0赞

目录

准备阶段

准备学生服务器

准备汽车服务器编辑

安装axios

一、Vue脚手架配置代理

代理服务器

Vue脚手架配置代理(一次性可以配置多个)

vue.config.js

App.vue

运行结果


总结

方法一

在vue.config.js中添加如下配置:

  1. devServer:{
  2. proxy:"http://localhost:5000"
  3. }

优点:配置简单,请求资源时直接发给前端(8080)即可

缺点:不能配置多个代理,不能灵活的控制请求是否走代理

工作方式:若按照上述配置代理,当请求了前端不存在的资源时,那么该请求会转发给服务器(优先匹配前端资源)

方法二

编写vue.config.js配置具体代理规则

3bdff9849602461196f99ee4a31a55dc.png

优点:可以配置多个代理,且可以灵活的控制请求是否走代理(想走代理加前缀,不想走不加)

缺点:配置略微繁琐,请求资源时必须加前缀

#

准备阶段

准备学生服务器

2fc83ae83c4a41dd93e04fdb1e08bcbb.png

  1. const express = require('express')
  2. const app = express()
  3. app.use((request,response,next)=>{
  4. console.log('有人请求服务器1了');
  5. // console.log('请求来自于',request.get('Host'));
  6. // console.log('请求的地址',request.url);
  7. next()
  8. })
  9. app.get('/students',(request,response)=>{
  10. const students = [
  11. {id:'001',name:'tom',age:18},
  12. {id:'002',name:'jerry',age:19},
  13. {id:'003',name:'tony',age:120},
  14. ]
  15. response.send(students)
  16. })
  17. app.listen(5000,(err)=>{
  18. if(!err) console.log('服务器1启动成功了,请求学生信息地址为:http://localhost:5000/students');
  19. })

" class="reference-link">e84c6b38adba42bfa81e0fa2ad3b98fc.png

" class="reference-link">准备汽车服务器949b509a2d244f588cb75a666c44439b.png

  1. const express = require('express')
  2. const app = express()
  3. app.use((request,response,next)=>{
  4. console.log('有人请求服务器2了');
  5. next()
  6. })
  7. app.get('/cars',(request,response)=>{
  8. const cars = [
  9. {id:'001',name:'奔驰',price:199},
  10. {id:'002',name:'马自达',price:109},
  11. {id:'003',name:'捷达',price:120},
  12. ]
  13. response.send(cars)
  14. })
  15. app.listen(5001,(err)=>{
  16. if(!err) console.log('服务器2启动成功了,请求汽车信息地址为:http://localhost:5001/cars');
  17. })

安装axios

f1686d7c972e455399b7792da9181de2.png

一、Vue脚手架配置代理

比如我们在 http localhost 8080 向 http localhost 5000发送请求想得到数据 就会出现下面的跨域问题,下面我们来解决一下

62a75a9c2bce4106ad8375ea405ca55a.png

代理服务器

(粉色) (红色8080代表前端工作人员)(充当中介的作用)

" class="reference-link">53bd2d02b4564b3b9b49a3ee0219d33a.png

Vue脚手架配置代理(一次性可以配置多个)

vue.config.js

  1. const { defineConfig } = require('@vue/cli-service')
  2. module.exports = defineConfig({
  3. // transpileDependencies: false
  4. transpileDependencies: true,
  5. lintOnSave: false, //关闭eslint检查
  6. // 开启代理服务器方式一
  7. // devServer:{
  8. // port: 8080,
  9. // proxy: 'http://localhost:5000',
  10. // }
  11. // 开启代理服务器(方式二)
  12. devServer:{
  13. proxy:{
  14. // 这是一套配置
  15. // 'atguigu' 是请求前缀
  16. '/atguigu':{
  17. target:'http://localhost:5000',
  18. // /atguigu/students 当我们转发给5000服务器时,不能带有atguigu
  19. // '^/atguigu'正则表达式:匹配所有atguigu的字符串,
  20. // 这个地方是一个key:value的形式 将atguigu字符串转化成空白字符串
  21. pathRewrite:{'^/atguigu':''},
  22. // 用于支持websocket 客户端和服务端的一种通信方式
  23. ws:true,
  24. // true:不会如实回答自己的端口号是8080 假设对面是5000 我们请求之后这个代理服务器告诉他也是5000 但是其实并不是
  25. // false:会如实回答自己的端口号8080, 不会欺骗5000说自己是5000端口
  26. // 这个地方是true撒谎好,以防5000端口号做限制不让别的端口访问
  27. changeOrigin:true
  28. },
  29. // 第二套配置
  30. '/demo':{
  31. target:'http://localhost:5001',
  32. pathRewrite:{'^/demo':''},
  33. ws:true,
  34. changeOrigin:true
  35. }
  36. }
  37. }
  38. })

App.vue

  1. <template>
  2. <div >
  3. <button @click="getStudents">获取学生信息</button>
  4. <button @click="getCars">获取汽车信息</button>
  5. </div>
  6. </template>
  7. <script>
  8. import axios from 'axios'
  9. export default {
  10. name:'App',
  11. methods:{
  12. getStudents(){
  13. // axios.get('http://localhost:5000/students').then(
  14. // 不要再找5000要数据了 我们开启了代理服务器 就找8080就行
  15. axios.get('http://localhost:8080/atguigu/students').then(
  16. // 请求成功时的回调
  17. response =>{
  18. // console.log('请求成功了',response) 如果想要成功后的数据,这样直接写response是不行的
  19. // response只是一个响应对象 response.data 才是真正的data数据
  20. console.log('请求成功了',response.data)
  21. },
  22. // 请求失败时的回调
  23. error =>{
  24. // error.message 失败的原因
  25. console.log('请求失败了',error.message)
  26. }
  27. )
  28. },
  29. getCars(){
  30. axios.get('http://localhost:8080/demo/cars').then(
  31. response =>{
  32. console.log('请求成功了',response.data)
  33. },
  34. error =>{
  35. console.log('请求失败了',error.message)
  36. }
  37. )
  38. }
  39. }
  40. }
  41. </script>

运行结果

4c141e5cd2e341a9a32f5a69c2d71742.png

a0f57e7827ff40e3ac47c68200b42548.png

发表评论

表情:
评论列表 (有 0 条评论,210人围观)

还没有评论,来说两句吧...

相关阅读

    相关 安装vue脚手架

    1. vue2.0 1.vue - V 查看版本,如果出现版本号,之前已经安装过了。如果之前安装的版本是想要的版本,可以直接创建项目 2.npm unins