react腳手架配置路徑別名的方法
文章寫時react版本16.13.1
1輸入命令 npm run eject 在項目根目錄下生成config目錄
2在confilg下打開webpack.config.js文件找到如下位置
alias: { // Support React Native Web // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/ 'react-native': 'react-native-web', // Allows for better profiling with ReactDevTools ...(isEnvProductionProfile && { 'react-dom$': 'react-dom/profiling', 'scheduler/tracing': 'scheduler/tracing-profiling', }), ...(modules.webpackAliases || {}), },
3修改如下,然后重啟項目
alias: { // Support React Native Web // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/ 'react-native': 'react-native-web', // Allows for better profiling with ReactDevTools ...(isEnvProductionProfile && { 'react-dom$': 'react-dom/profiling', 'scheduler/tracing': 'scheduler/tracing-profiling', }), ...(modules.webpackAliases || {}), // 文件路徑別名 '@': path.resolve(__dirname, '../src'), '@view': path.resolve(__dirname, '../src/view'), },
配置代理:
簡單版配置:
package.json中直接添加:"proxy": "http://localhost:4000"
完整版本配置:
(1).下載:yarn add http-proxy-middleware
(2).在src目錄下創(chuàng)建:setupProxy.js,內(nèi)容如下:
const proxy = require('http-proxy-middleware') module.exports = function(app) { app.use( proxy.createProxyMiddleware('/api', { //帶有api是需要轉(zhuǎn)發(fā)的請求 target: 'http://localhost:4000', // 這里是服務(wù)器地址 changeOrigin: true,//值為布爾值, 為true時, 本地就會虛擬一個服務(wù)器接收你的請求并代你發(fā)送該請求, pathRewrite: {'^/api': ''} }) ) }
3.備注:react腳手架的配置代理后,在請求資源時會優(yōu)先請求前端資源,若沒有再請求后端資源。
到此這篇關(guān)于react腳手架配置路徑別名的方法的文章就介紹到這了,更多相關(guān)react腳手架配置路徑別名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript滿天星導(dǎo)航欄實現(xiàn)方法
本篇文章給大家分享了JavaScript滿天星導(dǎo)航欄實現(xiàn)方法,以前也介紹過很多關(guān)于特效導(dǎo)航的制作方法,對此有興趣的朋友參考學習下。2018-03-03ibm官方資料把應(yīng)用程序從 Internet Explorer 遷移到 Mozilla
使特定于 Internet Explorer 的 Web 應(yīng)用程序在 Mozilla 上運行時,您遇到過麻煩嗎?本文討論了將應(yīng)用程序遷移到基于開源 Mozilla 瀏覽器上時的常見問題。首先討論跨瀏覽器開發(fā)的基本技術(shù),然后介紹克服 Mozilla 和 Internet Explorer 之間差異的策略。2008-04-04ToolTip 通過Js實現(xiàn)代替超鏈接中的title效果
ToolTip 通過Js實現(xiàn)代替超鏈接中的title效果,需要的朋友可以參考下。2011-04-04微信小程序?qū)崿F(xiàn)密碼顯示與隱藏的睜眼閉眼功能
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)密碼顯示與隱藏的睜眼閉眼功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-02-02談?wù)凧avaScript類型系統(tǒng)之Math
Math 對象并不像 Date 和 String 那樣是對象的類,因此沒有構(gòu)造函數(shù) Math(),像 Math.sin() 這樣的函數(shù)只是函數(shù),不是某個對象的方法。您無需創(chuàng)建它,通過把 Math 作為對象使用就可以調(diào)用其所有屬性和方法2016-01-01