vue項目打包發(fā)布后接口報405錯誤的解決
vue項目打包發(fā)布后接口報405
vue項目前端做了代理打包后后臺不識別報405 not allowed
vue.config.js文件配置
? devServer: { ? ? // host: "0.0.0.0", //項目運行時的本地地址 ? ? // port: 8880, // 端口號 ? ? //proxy:{'/api':{}},代理器中設置/api,項目中請求路徑為/api的替換為target ? ? proxy: { ? ? ? '/api': { ? ? ? ? // target: "http://192.168.0.249:19029",//代理地址,這里設置的地址會代替axios中設置的baseURL ? ? ? ? target: process.env.VUE_APP_BASEURL_API,//代理地址,這里設置的地址會代替axios中設置的baseURL ? ? ? ? changeOrigin: true,// 如果接口跨域,需要進行這個參數(shù)配置 ? ? ? ? //ws: true, // proxy websockets ? ? ? ? //pathRewrite方法重寫url ? ? ? ? pathRewrite: { ? ? ? ? ? '^/api': "/" ? ? ? ? ? //pathRewrite: {'^/api': '/'} 重寫之后url為 http://192.168.1.16:8085/xxxx ? ? ? ? ? //pathRewrite: {'^/api': '/api'} 重寫之后url為 http://192.168.1.16:8085/api/xxxx ? ? ? ? }, ? ? }, ? ? https: false, // https:{type:Boolean} ? ? disableHostCheck: true, ? ? open: true, //配置自動啟動瀏覽器 ? },
default.conf文件配置
server { ? ? listen ? ? ? 9000; ? ? server_name ?localhost; ? ? #charset koi8-r; ? ? #access_log ?/var/log/nginx/log/host.access.log ?main; ? ? location / { ? ? ? ? root ? /usr/share/nginx/html; ? ? ? ? index ?index.html index.htm; ? ? ? ? try_files ?$uri $uri/ ?@router; ? ? } ? ? location /api { ? ? ? ?rewrite ^/api/$ ?permanent; ? ? ? ?proxy_pass http://192.168.0.253:30001/warehouse/; ? ? ? ?proxy_redirect default; ? ? ? ?proxy_set_header Host $host; ? ? ? ?proxy_set_header X-Real-IP $remote_addr; ? ? ? ?proxy_set_header REMOTE-HOST $remote_addr; ? ? ? ?proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ? ? } ? ? #error_page ?404 ? ? ? ? ? ? ?/404.html; ? ? # redirect server error pages to the static page /50x.html ? ? # ? ? error_page ? 500 502 503 504 ?/50x.html; ? ? location = /50x.html { ? ? ? ? root ? html; ? ? } ? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80 ? ? # ? ?? ? ? #location ~ \.php$ { ? ? # ? ?proxy_pass ? http://127.0.0.1; ? ? #} ?# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 ? ? # ? ? #location ~ \.php$ { ? ? # ? ?root ? ? ? ? ? html; ? ? # ? ?fastcgi_pass ? 127.0.0.1:9000; ? ? # ? ?fastcgi_index ?index.php; ? ? # ? ?fastcgi_param ?SCRIPT_FILENAME ?/scripts$fastcgi_script_name; ? ? # ? ?include ? ? ? ?fastcgi_params; ? ? #} ? ? location @router { ? ? ? ? rewrite ^.*$ /index.html last; ? ? } ? ? # deny access to .htaccess files, if Apache's document root ? ? # concurs with nginx's one ? ? # ? ? #location ~ /\.ht { ? ? # ? ?deny ?all; ? ? #} }
vue項目打包之后接口出現(xiàn)錯誤問題
錯誤信息
這是新建一個項目還原問題,node簡單寫了個數(shù)據(jù)返回
關鍵代碼
const express = require('express') const app = express(); // 解決跨域問題 app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header('Access-Control-Allow-Headers', 'Content-Type'); next(); }) // 調用接口直接返回一個數(shù)組 app.get('/getData', (req, res) => { res.send([ { id: 1, name: 'GAI' }, { id: 2, name: 'keyNg' }, { id: 3, name: '閃火' } ]) })
// api/index.js import axios from 'axios' export function getData() { return axios({ url: 'api/getData', method: 'get' }) }
// home.vue mounted() { getData().then(res => { console.log(res); }) },
打包前
打包后
解決方式
設置環(huán)境變量
引用一句官網(wǎng)原話
請注意,只有 NODE_ENV,BASE_URL 和以 VUE_APP_ 開頭的變量將通過 webpack.DefinePlugin 靜態(tài)地嵌入到客戶端側的代碼中。這是為了避免意外公開機器上可能具有相同名稱的私鑰。
1.根目錄新增.env.development文件(會在開發(fā)環(huán)境被載入)
// .env.development VUE_APP_TITLE = '溫情dev' VUE_APP_ENV = 'dev' VUE_APP_BASE_URL = 'http://localhost:3000'
2.根目錄新增.env.production文件(會在生產環(huán)境被載入)
// .env.production VUE_APP_TITLE = '溫情pro' VUE_APP_ENV = 'pro' VUE_APP_BASE_URL = 'http://localhost:3000'
3.改一下 axios 請求方法
// api/index // 這里只是簡單解決一下問題 // 重點就是把開發(fā)環(huán)境和生產環(huán)境請求地址區(qū)分開來就可以了, 根據(jù)實際情況自行改動 import axios from 'axios' let baseURL = ''; // process.env.VUE_APP_ENV拿到我們在前面設置的模式, // 如果現(xiàn)在是開發(fā)環(huán)境會使用`.env.development`里面設置的環(huán)境變量等于`dev` // 如果現(xiàn)在是生產環(huán)境會使用`.env.production`里面設置的環(huán)境變量等于`pro` if(process.env.VUE_APP_ENV === 'dev') { ? ? baseURL = '/api'; } else { ? ? baseURL = process.env.VUE_APP_BASE_URL } export function getData() { ? ? return axios({ ? ? ? ? url: `${baseURL}/getData`, ? ? ? ? method: 'get' ? ? }) }
小提示: .env.development和.env.production文件修改之后記得重新跑一下項目
總結:區(qū)分開發(fā)模式
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
elementUI如何動態(tài)給el-tree添加子節(jié)點數(shù)據(jù)children詳解
element-ui 目前基本成為前端pc網(wǎng)頁端標準ui框架,下面這篇文章主要給大家介紹了關于elementUI如何動態(tài)給el-tree添加子節(jié)點數(shù)據(jù)children的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-11-11vue3+vite自定義封裝vue組件發(fā)布到npm包的全過程
當市面上主流的組件庫不能滿足我們業(yè)務需求的時候,那么我們就有必要開發(fā)一套屬于自己團隊的組件庫,下面這篇文章主要給大家介紹了關于vue3+vite自定義封裝vue組件發(fā)布到npm包的相關資料,需要的朋友可以參考下2022-09-09vue.js模仿京東省市區(qū)三級聯(lián)動的選擇組件實例代碼
選擇省市區(qū)是我們大家在填寫地址的時候經(jīng)常會遇到的一個功能,下面這篇文章主要給大家介紹了關于利用vue.js模仿實現(xiàn)京東省市區(qū)三級聯(lián)動選擇組件的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11electron-vue?運行報錯?Object.fromEntries?is?not?a?function
Object.fromEntries()?是?ECMAScript?2019?新增的一個靜態(tài)方法,用于將鍵值對列表(如數(shù)組)轉換為對象,如果在當前環(huán)境中不支持該方法,可以使用?polyfill?來提供類似功能,接下來通過本文介紹electron-vue?運行報錯?Object.fromEntries?is?not?a?function的解決方案2023-05-05在vue中通過render函數(shù)給子組件設置ref操作
這篇文章主要介紹了在vue中通過render函數(shù)給子組件設置ref操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11