React如何利用相對于根目錄進行引用組件詳解
前言
本文主要給大家介紹了關(guān)于React利用相對于根目錄進行引用組件的相關(guān)內(nèi)容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
在對自己開發(fā)的組件中經(jīng)常會做諸如以下的引用:
import genFetchEntryListArgs from '../../../utils/table/genFetchEntryListArgs';
import { parseQuery, stringifyQuery } from '../../../utils/query';
import mapMyToProps from '../../../utils/connect/mapMyToProps';
import genPagination from '../../../utils/table/pagination';
import handleConfirm from '../../../utils/handleConfirm';
import getBaseQuery from '../../../utils/getBaseQuery';
import setSortQuery from '../../../utils/setSortQuery';
import handleError from '../../../utils/handleError';
import injectProto from '../../../utils/injectProto';
import injectApi from '../../../utils/injectApi';
import querySchema from './querySchema';
import genColumns from './genColumns';
這樣使用相對路徑引用雖然是比較常見的做法,不過在中大型項目中,引入的組件較多時,寫起來也是極其蛋疼的。
當然,我們可以通過使用 webpack 中的 resolve.alias 配置別名,將某些文件目錄配置成固定的引入。
例如上面的示例,我們可以將 utils 文件夾設(shè)置成一個 utils 別名,以后就可以只需要將 utils 引入就行了,而不需要寫一坨 ../../../。
配置設(shè)置如下:
const path = require('path');
module.exports = {
...
resolve: {
alias: {
'utils': path.resolve(__dirname, '../src/utils'),
}
},
...
};
最上面的示例經(jīng)過改寫之后,應(yīng)該如此:
import genFetchEntryListArgs from '../../../utils/table/genFetchEntryListArgs';
import { parseQuery, stringifyQuery } from 'utils/query';
import mapMyToProps from 'utils/connect/mapMyToProps';
import genPagination from 'utils/table/pagination';
import handleConfirm from 'utils/handleConfirm';
import getBaseQuery from 'utils/getBaseQuery';
import setSortQuery from 'utils/setSortQuery';
import handleError from 'utils/handleError';
import injectProto from 'utils/injectProto';
import injectApi from 'utils/injectApi';
import querySchema from './querySchema';
import genColumns from './genColumns';
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
詳解React setState數(shù)據(jù)更新機制
這篇文章主要介紹了React setState數(shù)據(jù)更新機制的相關(guān)資料,幫助大家更好的理解和學習使用React框架,感興趣的朋友可以了解下2021-04-04
React + Threejs + Swiper 實現(xiàn)全景圖效果的完整代碼
全景圖效果非常漂亮給人帶來極好的用戶體驗效果,那么基于前端開發(fā)如何實現(xiàn)這種效果呢,下面小編給大家?guī)砹薘eact + Threejs + Swiper 實現(xiàn)全景圖效果,感興趣的朋友一起看看吧2021-06-06
react的ui庫antd中form表單使用SelectTree反顯問題及解決
這篇文章主要介紹了react的ui庫antd中form表單使用SelectTree反顯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
ReactJs實現(xiàn)樹形結(jié)構(gòu)的數(shù)據(jù)顯示的組件的示例
本篇文章主要介紹了ReactJs實現(xiàn)樹形結(jié)構(gòu)的數(shù)據(jù)顯示的組件的示例,非常具有實用價值,需要的朋友可以參考下2017-08-08

