ES6 import from root

后端 未结 8 1669
别跟我提以往
别跟我提以往 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 18:46

    With webpack you can also make paths starting with for example ~ resolve to the root, so you can use import Loading from '~/components/Loading';:

    resolve: {
      extensions: ['.js'],
      modules: [
        'node_modules', 
        path.resolve(__dirname + '/app')
      ],
      alias: {
        ['~']: path.resolve(__dirname + '/app')
      }
    }
    

    The trick is using the javascript bracket syntax to assign the property.

提交回复
热议问题