您好,登錄后才能下訂單哦!
這篇文章主要介紹了react腳手架如何配置路徑別名,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
react版本16.13.1
1輸入命令 npm run eject 在項(xiàng)目根目錄下生成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修改如下,然后重啟項(xiàng)目
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)先請求前端資源,若沒有再請求后端資源。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“react腳手架如何配置路徑別名”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。