Detailed explanation of React's use of referencing component instances relative to the root directory

小云云
Release: 2018-01-27 11:19:13
Original
2534 people have browsed it

This article mainly introduces to you the relevant information on how to use React to reference components relative to the root directory. The article introduces it in great detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it Let's learn with the editor below, I hope it can help everyone.

In the components developed by myself, I often make references such as the following:


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';
Copy after login

Although using relative path references in this way is It is a relatively common approach, but in medium and large projects, when a lot of components are introduced, it is extremely painful to write.

Of course, we can configure certain file directories as fixed imports by usingresolve.aliasin webpack to configure aliases.

For example, in the above example, we can set the utils folder as a utils alias. In the future, we can only import utils without writing a bunch of ../../../.

The configuration settings are as follows:


const path = require('path'); module.exports = { ... resolve: { alias: { 'utils': path.resolve(__dirname, '../src/utils'), } }, ... };
Copy after login

After the top example has been rewritten, it should look like this:


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';
Copy after login

Related recommendations:

php Several methods to obtain the physical path of the website root directory (recommended) Tips

The above is the detailed content of Detailed explanation of React's use of referencing component instances relative to the root directory. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!