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: {
// 跨域的寫法
proxy: {
'/api': {
target: 'http://nvzu.xxx.cn/', // 實(shí)際請求地址
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
},
},
// 不跨域的寫法
/* 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; */頁面使用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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中ElementUI結(jié)合transform使用時如何修復(fù)el-select彈框定位不準(zhǔn)確問題
這篇文章主要介紹了Vue中ElementUI結(jié)合transform使用時如何修復(fù)el-select彈框定位不準(zhǔn)確問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
vue-router配合ElementUI實(shí)現(xiàn)導(dǎo)航的實(shí)例
下面小編就為大家分享一篇vue-router配合ElementUI實(shí)現(xiàn)導(dǎo)航的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
vue路由第二次進(jìn)入頁面created和mounted不執(zhí)行問題及解決
這篇文章主要介紹了vue路由第二次進(jìn)入頁面created和mounted不執(zhí)行問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
利用vue-router實(shí)現(xiàn)二級菜單內(nèi)容轉(zhuǎn)換
這篇文章主要介紹了如何利用vue-router實(shí)現(xiàn)二級菜單內(nèi)容轉(zhuǎn)換,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
element-ui和vue表單(對話框)驗(yàn)證提示語(殘留)清除操作
這篇文章主要介紹了element-ui和vue表單(對話框)驗(yàn)證提示語(殘留)清除操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

