ES6 import from root

后端 未结 8 1686
别跟我提以往
别跟我提以往 2020-12-08 18:28

I\'m currently playing around with React Native. I\'m trying to structure my app, however it\'s starting to get messy with imports.

--app/
    -- /components         


        
8条回答
  •  臣服心动
    2020-12-08 19:06

    If you are using Webpack you can configure it via the resolve property to resolve a your import path.

    Webpack 1

    resolve: {
      root: [
        path.resolve(__dirname  + '/src')
      ]
    }......
    

    Webpack 2

    resolve: {
      modules: [
        path.resolve(__dirname + '/src'),
        path.resolve(__dirname + '/node_modules')
      ]
    }.....
    

    After that you can use

    import configureStore from "store/configureStore";
    

    instead of the:

    import configureStore from "../../store/configureStore";
    

    Webpack will configure your import path from the passed resolve param.

    The same stuff you can do with System.js loader but with it's own config param (it's can be map or path. Check it in the System.js documentation) (if you would like to use it. It's mostly for a Angular 2 case. But I suggest: don't use standard System.js even if you are working with ng2. Webpack is much better).

提交回复
热议问题