Vue3跨域解決方案實(shí)例詳解
vue項(xiàng)目配置代理
vue.config.js
const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ transpileDependencies: true, devServer:{ proxy:{ '/api':{ target: 'http://xx.xx.xxx.xx', changeOrigin:true, pathRewrite: { '^/api': '' } } } } })
如果是用vue3+ts則在vue.config.ts中添加以下代碼:
server: { // 跨域的寫(xiě)法 proxy: { '/api': { target: 'http://nvzu.xxx.cn/', // 實(shí)際請(qǐng)求地址 changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ""), }, }, }, // 不跨域的寫(xiě)法 /* server: { host: '192.168.1.195' // 0.0.0.0 }, */
axios.js
"use strict"; import axios from "axios"; // Full config: https://github.com/axios/axios#request-config axios.defaults.baseURL = '/api' || ''; // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN; // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; let config = { // 這里的api就是獲取configJS中的地址 baseURL: '/api' // timeout: 60 * 1000, // Timeout // withCredentials: true, // Check cross-site Access-Control }; const _axios = axios.create(config); _axios.interceptors.request.use( function(config) { // Do something before request is sent return config; }, function(error) { // Do something with request error return Promise.reject(error); } ); // Add a response interceptor _axios.interceptors.response.use( function(response) { // Do something with response data return response; }, function(error) { // Do something with response error return Promise.reject(error); } ); export default{ install:function(app){ app.config.globalProperties.axios = _axios; app.config.globalProperties.$translate = (key) =>{ return key } } } /* Plugin.install = function(Vue) { Vue.axios = _axios; window.axios = _axios; Object.defineProperties(Vue.prototype, { axios: { get() { return _axios; } }, $axios: { get() { return _axios; } }, }); }; Vue.use(Plugin) export default Plugin; */
頁(yè)面使用proxy.axios.get/post進(jìn)行獲取跨域接口
<template> <div class="home"> <img alt="Vue logo" src="../assets/logo.png"> <HelloWorld msg="Welcome to Your Vue.js App"/> </div> </template> <script> import {getCurrentInstance} from 'vue' // 引入Vue3中的getCurrentInstance // @ is an alias to /src import HelloWorld from '@/components/HelloWorld.vue' export default { name: 'HomeView', mounted(){ const {proxy} = getCurrentInstance(); console.log(proxy); // Axios.get proxy.axios.get('/index_category/data').then((e)=>{ console.log(e); }) }, components: { HelloWorld } } </script>
到此這篇關(guān)于Vue3跨域解決方案實(shí)例詳解的文章就介紹到這了,更多相關(guān)Vue3跨域解決方案內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 利用uniapp+vue3+js適配微信隱私協(xié)議開(kāi)發(fā)指南
- uniapp+vue3路由跳轉(zhuǎn)傳參的實(shí)現(xiàn)
- uniapp-vue3-彈出選擇組件wo-pop-selector使用示例
- Vue3全局配置Axios并解決跨域請(qǐng)求問(wèn)題示例詳解
- vue3之a(chǎn)xios跨域請(qǐng)求問(wèn)題及解決
- vue3 vite配置跨域及不生效問(wèn)題解決
- Vue3?跨域配置devServer的參數(shù)和設(shè)置方法
- Vue3.0?axios跨域請(qǐng)求代理服務(wù)器配置方式
- uniapp Vue3中如何解決web/H5網(wǎng)頁(yè)瀏覽器跨域的問(wèn)題
相關(guān)文章
Vue中ElementUI結(jié)合transform使用時(shí)如何修復(fù)el-select彈框定位不準(zhǔn)確問(wèn)題
這篇文章主要介紹了Vue中ElementUI結(jié)合transform使用時(shí)如何修復(fù)el-select彈框定位不準(zhǔn)確問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01vue-router配合ElementUI實(shí)現(xiàn)導(dǎo)航的實(shí)例
下面小編就為大家分享一篇vue-router配合ElementUI實(shí)現(xiàn)導(dǎo)航的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02vue路由第二次進(jìn)入頁(yè)面created和mounted不執(zhí)行問(wèn)題及解決
這篇文章主要介紹了vue路由第二次進(jìn)入頁(yè)面created和mounted不執(zhí)行問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12利用vue-router實(shí)現(xiàn)二級(jí)菜單內(nèi)容轉(zhuǎn)換
這篇文章主要介紹了如何利用vue-router實(shí)現(xiàn)二級(jí)菜單內(nèi)容轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11element-ui和vue表單(對(duì)話(huà)框)驗(yàn)證提示語(yǔ)(殘留)清除操作
這篇文章主要介紹了element-ui和vue表單(對(duì)話(huà)框)驗(yàn)證提示語(yǔ)(殘留)清除操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09