Vue代理報錯404問題及解決(vue配置proxy)
Vue代理報錯404問題
問題描述:
代理后出現(xiàn)404:
第一種路徑拼接 /api 情況
const path = require('path'); function resolve(dir) { return path.resolve(__dirname, dir) } module.exports = { publicPath: process.env.NODE_ENV === "production" ? "./" : "/", assetsDir: "assets", configureWebpack: { resolve: { extensions: ['.js', '.vue', '.json'], alias: { '@': resolve('src'), 'assets': resolve('src/assets'), 'css': resolve('src/assets/css'), 'images': resolve('src/assets/images'), 'views': resolve('src/views'), 'components': resolve('src/components'), 'api': resolve('src/api'), 'mixins': resolve('src/mixins'), 'store': resolve('src/store'), 'service': resolve('src/service'), } } }, devServer: { port: 8081, hot: true, // open: 'Chrome', proxy: { // change xxx-api/login => mock/login // detail: https://cli.vuejs.org/config/#devserver-proxy '/api': { target: 'https://www.xxxxxx.com/', changeOrigin: true, secure: true, pathRewrite: { '^/api': '/' } }, }, }, css: { loaderOptions: { css: {}, postcss: { plugins: [ require('postcss-px2rem')({ remUnit: 192 }) ] } } }, productionSourceMap: false, };
使用如下:
結(jié)果如下:
缺點 :打包后的請求路徑會多個 /api
第二種路徑不變情況
這種寫法也可以:
注意:這時候就按原來的路徑就可以了;
結(jié)果如下:
缺點:配置多個代理要注意代理的先后順序;
注意點
代理的順序:從前往后匹配的,先匹配到哪個就用哪個;
新增說明:配置多個代理怎么搞?
配置多個代理怎么搞?
'use strict' const path = require('path') // const SkeletonWebpackPlugin = require('vue-skeleton-webpack-plugin') const defaultSettings = require('./src/settings.js') function resolve(dir) { return path.join(__dirname, dir) } const name = defaultSettings.title || 'xxx' // page title // If your port is set to 80, // use administrator privileges to execute the command line. // For example, Mac: sudo npm run // You can change the port by the following methods: // port = 9527 npm run dev OR npm run dev --port = 9527 const port = process.env.port || process.env.npm_config_port || 9527 // dev port // All configuration item explanations can be find in https://cli.vuejs.org/config/ module.exports = { /** * You will need to set publicPath if you plan to deploy your site under a sub path, * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/, * then publicPath should be set to "/bar/". * In most cases please use '/' !!! * Detail: https://cli.vuejs.org/config/#publicpath */ // publicPath: process.env.ENV === 'production' ? '/' : '/', publicPath: process.env.ENV === 'production' ? '/' : '/', outputDir: 'dist', assetsDir: 'static', lintOnSave: process.env.NODE_ENV === 'development', productionSourceMap: false, devServer: { host: 'localhost',//target host port: port, overlay: { warnings: false, errors: true }, proxy: { [process.env.VUE_APP_BASE_BRAND_API]: { target: 'http://10.44.62.64:8090', changeOrigin: true, pathRewrite: { ['^' + process.env.VUE_APP_BASE_BRAND_API]: '' } }, // change xxx-api/login => mock/login // detail: https://cli.vuejs.org/config/#devserver-proxy [process.env.VUE_APP_BASE_API]: { // target: 'http://localhost:3100', target: 'http://xx.xxx.xx.x:3100', // target: `http://127.0.0.1:${port}/mock`, changeOrigin: true, pathRewrite: { ['^' + process.env.VUE_APP_BASE_API]: '' } }, }, // proxy:{ // // 當(dāng)你請求是以/api開頭的時候,則我?guī)湍愦碓L問到http://localhost:3000 // // 例如: // // /api/users http://localhost:3000/api/users // // 我們真是服務(wù)器接口是沒有/api的 // "":{ // target:"http://localhost:31009", // changeOrigin: true,// 如果接口跨域,需要進行這個參數(shù)配置 // pathRewrite:{"^":""} // } // } // after: require('./mock/mock-server.js') }, configureWebpack: { // provide the app's title in webpack's name field, so that // it can be accessed in index.html to inject the correct title. name: name, resolve: { alias: { '@': resolve('src') } } }, // css: { // extract: true // 是否使用css分離插件 ExtractTextPlugin(開啟骨架屏必須這個配置) // }, chainWebpack(config) { config.plugin('html').tap(args => { // 添加初始化變量 args[0].monitorId = process.env.VUE_APP_MONITOR_ID return args }) config.plugins.delete('preload') // TODO: need test config.plugins.delete('prefetch') // TODO: need test // set svg-sprite-loader config.module .rule('svg') .exclude.add(resolve('src/icons')) .end() config.module .rule('icons') .test(/\.svg$/) .include.add(resolve('src/icons')) .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) .end() // set preserveWhitespace config.module .rule('vue') .use('vue-loader') .loader('vue-loader') .tap(options => { options.compilerOptions.preserveWhitespace = true return options }) .end() config // https://webpack.js.org/configuration/devtool/#development .when(process.env.NODE_ENV === 'development', config => config.devtool('cheap-source-map') ) config .when(process.env.NODE_ENV !== 'development', config => { config .plugin('ScriptExtHtmlWebpackPlugin') .after('html') .use('script-ext-html-webpack-plugin', [{ // `runtime` must same as runtimeChunk name. default is `runtime` inline: /runtime\..*\.js$/ }]) .end() config .optimization.splitChunks({ chunks: 'all', cacheGroups: { libs: { name: 'chunk-libs', test: /[\\/]node_modules[\\/]/, priority: 10, chunks: 'initial' // only package third parties that are initially dependent }, elementUI: { name: 'chunk-elementUI', // split elementUI into a single package priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm }, commons: { name: 'chunk-commons', test: resolve('src/components'), // can customize your rules minChunks: 3, // minimum common number priority: 5, reuseExistingChunk: true } } }) config.optimization.runtimeChunk('single') } ) // 骨架屏配置 // config // .plugin('SkeletonWebpackPlugin') // .use(new SkeletonWebpackPlugin({ // webpackConfig: { // entry: { // app: resolve('src/skeleton.js'), // } // }, // minimize: true, // quiet: true, // router: { // mode: 'history', // routes: [{ // path: '*', // skeletonId: 'common-skeleton' // }] // } // })) } }
注意.env.development中的配置
NODE_ENV = 'development' # just a flag ENV = 'development' # base api(當(dāng)使用mock.js時,切換dev-api) # VUE_APP_BASE_API = '/dev-api' VUE_APP_BASE_API = '' # vue-cli uses the VUE_CLI_BABEL_TRANSPILE_MODULES environment variable, # to control whether the babel-plugin-dynamic-import-node plugin is enabled. # It only does one thing by converting all import() to require(). # This configuration can significantly increase the speed of hot updates, # when you have a large number of pages. # Detail: https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/babel-preset-app/index.js VUE_CLI_BABEL_TRANSPILE_MODULES = true # 圖片和附件上傳文件夾 VUE_APP_OSS_FOLDER='test/xxx/' # 品牌項目 (這里為了代理,要多加個 /brand,當(dāng)然可以自己定義) VUE_APP_BASE_BRAND_API = '/brand'
配置了 .env.development
中的代理路徑后,這樣api接口js文件中就不用多寫個 /brand
,不用寫成 /brand/saveOrUpdate
,請求路徑也會存在 /brand
,baseURL拼接的原因,這樣打包也并不影響,畢竟打包的是生產(chǎn)環(huán)境的路徑;
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue keep-alive實現(xiàn)多組件嵌套中個別組件存活不銷毀的操作
這篇文章主要介紹了vue keep-alive實現(xiàn)多組件嵌套中個別組件存活不銷毀的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue中利用three.js實現(xiàn)全景圖的完整示例
這篇文章主要給大家介紹了關(guān)于vue中利用three.js實現(xiàn)全景圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12