Vue3跨域解決方案實例詳解
更新時間:2023年01月26日 12:10:28 作者:Nanchen_42
這篇文章主要介紹了Vue3跨域解決方案詳解,需要的朋友可以參考下
vue項目配置代理
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/', // 實際請求地址
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進行獲取跨域接口
<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跨域解決方案實例詳解的文章就介紹到這了,更多相關(guān)Vue3跨域解決方案內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue v-model實現(xiàn)自定義樣式多選與單選功能
這篇文章主要介紹了vue v-model實現(xiàn)自定義樣式多選與單選功能所遇到的問題,本文給大家?guī)砹私鉀Q思路和實現(xiàn)代碼,需要的朋友可以參考下2018-07-07
Vue3.0路由跳轉(zhuǎn)攜帶參數(shù)的示例詳解
這篇文章主要介紹了Vue3.0路由跳轉(zhuǎn)攜帶參數(shù)的示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
vue 實現(xiàn)axios攔截、頁面跳轉(zhuǎn)和token 驗證
這篇文章主要介紹了vue 實現(xiàn)axios攔截、頁面跳轉(zhuǎn)和token 驗證,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Vue中$router.push()路由切換及如何傳參和獲取參數(shù)
這篇文章主要給大家介紹了關(guān)于Vue中$router.push()路由切換及如何傳參和獲取參數(shù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用vue具有一定的參考學習價值,需要的朋友可以參考下2023-03-03

