【发布时间】:2020-10-10 03:35:46
【问题描述】:
我正在尝试为服务和组件使用路径别名,以避免混乱的导入。我的服务位于 src/app/services 文件夹下。我有一个 AppService 文件 app.service.ts。我在 tsconfig.json 中添加了以下代码。
{
"compilerOptions": {
"baseUrl": "./src/app",
"paths": {
"@services/*": ["services/*"]
},
我在我的组件类中导入了服务,比如
import { AppService } from '@services/app.service';
它不起作用。它抛出错误 TS2307:找不到模块 '@services/app.service' 或其相应的类型声明。这里有什么问题?
【问题讨论】:
-
我通常会将 baseUrl 保留为
'.'并使用“src/app/services”。此外,在生成 lib angular 时会创建两个路径别名,一个不带通配符 ("@services": ["src/app/services"]),另一个带通配符 ("@services/*": ["src/app/services/*"])。理论上你的应该可以工作,希望它有帮助。考虑在services的根目录下添加一个index.ts文件,这样你就可以import {...} from '@services' -
感谢您的回复。我仍然收到相同的错误消息。
{ "compilerOptions": { "baseUrl": ".", "paths": { "@services/*": ["src/app/services/*"] },
标签: angular typescript eclipse