经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
Vue3跨域解决方案实例详解
来源:jb51  时间:2023/1/28 8:43:07  对本文有异议

vue项目配置代理

vue.config.js

  1. const { defineConfig } = require('@vue/cli-service')
  2. module.exports = defineConfig({
  3. transpileDependencies: true,
  4. devServer:{
  5. proxy:{
  6. '/api':{
  7. target: 'http://xx.xx.xxx.xx',
  8. changeOrigin:true,
  9. pathRewrite: {
  10. '^/api': ''
  11. }
  12. }
  13. }
  14. }
  15. })

如果是用vue3+ts则在vue.config.ts中添加以下代码:

  1. server: {
  2. // 跨域的写法
  3. proxy: {
  4. '/api': {
  5. target: 'http://nvzu.xxx.cn/', // 实际请求地址
  6. changeOrigin: true,
  7. rewrite: (path) => path.replace(/^\/api/, ""),
  8. },
  9. },
  10. },
  11. // 不跨域的写法
  12. /* server: {
  13. host: '192.168.1.195'
  14. // 0.0.0.0
  15. }, */

axios.js

  1. "use strict";
  2. import axios from "axios";
  3. // Full config: https://github.com/axios/axios#request-config
  4. axios.defaults.baseURL = '/api' || '';
  5. // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
  6. // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  7. let config = {
  8. // 这里的api就是获取configJS中的地址
  9. baseURL: '/api'
  10. // timeout: 60 * 1000, // Timeout
  11. // withCredentials: true, // Check cross-site Access-Control
  12. };
  13. const _axios = axios.create(config);
  14. _axios.interceptors.request.use(
  15. function(config) {
  16. // Do something before request is sent
  17. return config;
  18. },
  19. function(error) {
  20. // Do something with request error
  21. return Promise.reject(error);
  22. }
  23. );
  24. // Add a response interceptor
  25. _axios.interceptors.response.use(
  26. function(response) {
  27. // Do something with response data
  28. return response;
  29. },
  30. function(error) {
  31. // Do something with response error
  32. return Promise.reject(error);
  33. }
  34. );
  35. export default{
  36. install:function(app){
  37. app.config.globalProperties.axios = _axios;
  38. app.config.globalProperties.$translate = (key) =>{
  39. return key
  40. }
  41. }
  42. }
  43. /* Plugin.install = function(Vue) {
  44. Vue.axios = _axios;
  45. window.axios = _axios;
  46. Object.defineProperties(Vue.prototype, {
  47. axios: {
  48. get() {
  49. return _axios;
  50. }
  51. },
  52. $axios: {
  53. get() {
  54. return _axios;
  55. }
  56. },
  57. });
  58. };
  59. Vue.use(Plugin)
  60. export default Plugin; */

页面使用proxy.axios.get/post进行获取跨域接口

  1. <template>
  2. <div class="home">
  3. <img alt="Vue logo" src="../assets/logo.png">
  4. <HelloWorld msg="Welcome to Your Vue.js App"/>
  5. </div>
  6. </template>
  7. <script>
  8. import {getCurrentInstance} from 'vue' // 引入Vue3中的getCurrentInstance
  9. // @ is an alias to /src
  10. import HelloWorld from '@/components/HelloWorld.vue'
  11. export default {
  12. name: 'HomeView',
  13. mounted(){
  14. const {proxy} = getCurrentInstance();
  15. console.log(proxy);
  16. // Axios.get
  17. proxy.axios.get('/index_category/data').then((e)=>{
  18. console.log(e);
  19. })
  20. },
  21. components: {
  22. HelloWorld
  23. }
  24. }
  25. </script>

到此这篇关于Vue3跨域解决方案实例详解的文章就介绍到这了,更多相关Vue3跨域解决方案内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号