vuecli3打包后出現(xiàn)跨域問題,前端配置攔截器無效的解決
打包后跨域問題,前端配置攔截器無效
問題
這幾天在把項(xiàng)目弄好,打包完成后發(fā)現(xiàn)之前cli配置的攔截器沒有在打包后沒起到作用,使用別的方法通過nginx反向代理進(jìn)行配置跨域。
解決方案
在nginx里面的nginx.config里面配置
配置如下
server {
listen 80;#監(jiān)聽端口
server_name localhost;#代理服務(wù)地址
add_header Access-Control-Allow-Origin *;
location / {
root C:/nginx-1.19.0/html/dist; #根目錄!!,把這里路徑設(shè)置為項(xiàng)目的根路徑
autoindex on; #開啟nginx目錄瀏覽功能
autoindex_exact_size off; #文件大小從KB開始顯示
charset utf-8; #顯示中文
add_header 'Access-Control-Allow-Origin' '*'; #允許來自所有的訪問地址
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, DELETE, OPTIONS'; #支持請求方式
add_header 'Access-Control-Allow-Headers' 'Content-Type,*';
}
#開始配置我們的反向代理
location /apis{//cli配置的接口名
rewrite ^/apis/(.*)$ /$1 break;
include uwsgi_params;
proxy_set_header Host $host;
proxy_set_header x-forwarded-for $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://*****:8080;//接口
}
location /topicByCate{//cli配置的接口名
rewrite ^/topicByCate/(.*)$ /$1 break;
include uwsgi_params;
proxy_set_header Host $host;
proxy_set_header x-forwarded-for $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass https://******.com;//接口
}
location @router {
rewrite ^.*$ /index.html last;
}
}
vue3處理跨域問題
在項(xiàng)目根目錄新建vue.config.js輸入
module.exports = {
? ? devServer: {
? ? ? ? proxy: {
? ? ? ? ? ? '/api': {
? ? ? ? ? ? ? ? target: 'http://www.example.com:81/', //接口域名,端口看各自設(shè)置的
? ? ? ? ? ? ? ? changeOrigin: true, ? ? ? ? ? ? //是否跨域
? ? ? ? ? ? ? ? ws: true, ? ? ? ? ? ? ? ? ? ? ? //是否代理 websockets
? ? ? ? ? ? ? ? secure: true, ? ? ? ? ? ? ? ? ? //是否https接口
? ? ? ? ? ? ? ? pathRewrite: { ? ? ? ? ? ? ? ? ?//路徑重置
? ? ? ? ? ? ? ? ? ? '^/api': ''
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
};如用到的是vite.config.js
則在這個文件添加
module.exports = {
? ? devServer: {
? ? ? ? proxy: {
? ? ? ? ? ? '/api': {
? ? ? ? ? ? ? ? target: 'http://www.example.com:81', //接口域名,端口看各自設(shè)置的
? ? ? ? ? ? ? ? changeOrigin: true, ? ? ? ? ? ? //是否跨域
? ? ? ? ? ? ? ? ws: true, ? ? ? ? ? ? ? ? ? ? ? //是否代理 websockets
? ? ? ? ? ? ? ? secure: true, ? ? ? ? ? ? ? ? ? //是否https接口
? ? ? ? ? ? ? ? pathRewrite: { ? ? ? ? ? ? ? ? ?//路徑重置
? ? ? ? ? ? ? ? ? ? '^/api': ''
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
};以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于vue的npm run dev和npm run build的區(qū)別介紹
這篇文章主要介紹了關(guān)于vue的npm run dev和npm run build的區(qū)別介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
VueJS設(shè)計(jì)與實(shí)現(xiàn)之淺響應(yīng)與深響應(yīng)詳解
這篇文章主要為大家介紹了VueJS設(shè)計(jì)與實(shí)現(xiàn)之淺響應(yīng)與深響應(yīng)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
前端處理axios請求下載后端返回的文件流代碼實(shí)例
使用axios可以很方便地獲取后端返回的文件流數(shù)據(jù),并在前端直接在瀏覽器下載,這篇文章主要給大家介紹了關(guān)于前端處理axios請求下載后端返回的文件流的相關(guān)資料,需要的朋友可以參考下2024-07-07
使用vue3重構(gòu)拼圖游戲的實(shí)現(xiàn)示例
這篇文章主要介紹了使用vue3重構(gòu)拼圖游戲的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Vue項(xiàng)目中ESLint配置超全指南(VScode)
ESLint是一個代碼檢查工具,用來檢查你的代碼是否符合指定的規(guī)范,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目中ESLint配置(VScode)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04

