2016-12-03 110 views
1

忽略我有一个tsconfig.json文件在我的项目的根目录:tsconfig.json通过Visual Studio代码

{ 
    "compilerOptions": { 
    "target": "ES5", 
    "module": "commonjs", 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "sourceMap": true, 
    "noEmitHelpers": true, 
    "baseUrl": ".", 
    "paths": { 
     "services/*": ["./src/app/shared/services/*"] 
    } 
    }, 
    "compileOnSave": false, 
    "buildOnSave": false, 
    "awesomeTypescriptLoaderOptions": { 
    "forkChecker": true, 
    "useWebpackText": true 
    } 
} 

我试图定义我的路径,所以我们并不需要在项目中使用相对路径。

例如:

我想,而不必键入获得import { ConfigService } from 'services/config';工作,:

import { ConfigService } from '../../shared/services/config/';

,但我不断收到中的WebPack错误,当我试试这个。

enter image description here

看起来它只是试图在/node_nodules看,而忽略tsconfig.json?我对吗?

我真的不知道我在做什么错。 Google搜寻了两个多小时并且疯了。

任何帮助,将不胜感激。

回答

2

注意事项:事实上,你是对的,Webpack不知道tsconfig文件,并尝试以自己的方式解决模块。为了在tsconfig中使用它,你需要一个TsConfigPathsPlugin插件。请参阅github上的issue

我认为你的路径属性设置不正确。你需要告诉打字机是这样的:

对于匹配模式“services /”的导入,将以下路径追加到 导入中(假设您的根目录是src之前的目录)“/ src/app/shared /”

"paths": { 
    "services/*": ["/src/app/shared/*"] 
} 

所以,当编译器得到你的‘服务/配置’进口,这将匹配的模式,然后创建这个路径‘根/ src目录/应用/共享/服务/配置’,试图解决它。

有关更多详细信息,请参阅打印稿module resolution文档。

+1

你完全正确,我在'webpack.config.js'中定义了'TsConfigPathsPlugin'插件并设置了'tsconfig.json',所有东西都开始工作了! –