在我的 React 项目中,我在 webpack 配置中定义了一个模块别名。我想开始转向 Typescript。
//我尽量简化设置
这是我在根文件夹中的 tsconfig.json
:
{
"compilerOptions": {
"target": "es6",
"module": "es6",
"moduleResolution": "node",
"jsx": "react",
"noImplicitAny": true,
"strictNullChecks": true,
"sourceMap": true,
"lib": [
"dom",
"es2015",
"es2016"
],
"baseUrl": "./src",
"paths": {
"app": ["./app"]
}
},
"include": [
"src/**/*"
]
}
这是我的项目结构:
[rootDir]
|
|- [src]
| |
| |-[app]
| |
| |-[components]
| | ... react components live here
| |- Test.tsx
| |- SomeComponent.tsx
| |- index.js (export { default as Test } from './Test')
|
|- tsconfig.json
|- webpack.config.js
我将 awesome-typescript-loader
与 Webpack 2 一起使用,我还在其中包含了插件来加载路径。我也使用 Visual Studio Code,它内置了 Typescript,但显示相同的错误。
在我的 Typescript 组件 SomeComponent.tsx
中,我想包含另一个组件,如下所示:
import * as React from 'react'
import { Test } from 'app/components'
我收到以下错误:
Cannot find module 'app/components'
最佳答案
解决方案是编辑路径配置以查找嵌套文件。
"baseUrl": ".",
"paths": {
"app*": ["src/app*"]
}
现在,当我添加 import { Test } from 'app/components'
时,Typescript 将查看 src/app/components/index.js
并且它有效。我还喜欢将 @
添加到别名中,以避免与其他模块发生冲突。像这样:
"@app*": ["src/app*"]
关于javascript - Typescript 找不到在 "paths"设置中定义的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44463100/