简体   繁体   中英

How to define a typescript import path relative to some root (using webpack)?

I'm writing a Vue application that has many components which are nested in different folders. My folder structure looks something like this:

src folder
+components folder 
++partA folder
+++A.vue
+++AA.ts
+++subA folder
++++AAA.ts
++partB
+++B.vue
+++BB.ts

Inside my typeScript files, I sometimes need to refer to one of the other components so I end up with code that looks like:

import { something } from '../../partB/BB'
import { another } from './subA/AAA'

Is there a way to setup webpack so that I can always specify the import path to my components relative to the root ( src ) folder rather than having a bunch of relative paths defined that are specific to the file which is using the import statement?

I found a solution by using webpack's resolve alias: https://webpack.js.org/configuration/resolve/

in conjunction with tsconfig.json ( https://www.typescriptlang.org/docs/handbook/compiler-options.html ). I needed to specify a baseUrl and paths to match what I have setup in my resolve alias.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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