简体   繁体   English

如何缩短导入路径

[英]How to shorten import path

I'm writing a React-Redux app now. 我正在写一个React-Redux应用程序。 And a question appeared in my mind. 我脑海中浮现出一个问题。 I often have to write such strings like: 我经常不得不写这样的字符串:

import * as mainActions from '../../../main/actions/main-actions';
import {wysiwygComponent} from '../../../../components.jsx';

So I'm kind of sick of these "../../..". 所以我有点讨厌这些“ ../../ ..”。 Is there any way to make components having global names or paths? 有什么方法可以使组件具有全局名称或路径?

This is when module loaders come in handy. 这是模块装载机派上用场的时候。

If you use Webpack 2.x, you can modify the Path Resolver Configuration like this 如果您使用Webpack 2.x,则可以像这样修改路径解析器配置

resolve: {
  modules: [
    path.resolve('./client'),
    path.resolve('./node_modules')
  ]
},

You specify a number of directories in modules which will turn 您在模块中指定了许多目录,这些目录将

import Header from '../../components/Header';
import TransactionForm from '../TransactionForm'; 

into

import Header from 'components/Header';
import TransactionForm from 'TransactionForm';

Reference : No Relative Path 参考没有相对路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM