我正在开发一个项目,该项目包括几个基于 Angular 8 的不同前端。每个前端都是一个单独的 Angular 8 项目,具有自己的文件夹子结构。它们的根文件夹都位于同一个主文件夹中。
theproject
|___ frontend1
|___ frontend2
...
为了避免重复代码并优化代码重构,我们决定将重复代码放入“基本”文件夹并通过在tsconfig.json
.
theproject
|___ frontend1/
|___ frontend2/
...
|___ base-ui/
我们从一个简单的类开始,该类表示在整个前端环境中使用的常量集合。此类位于文件constants.ts
夹中的文件内base-ui/constants
theproject
|___ frontend1/
|___ frontend2/
...
|___ base-ui/constants/constants.ts
这是我们的tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
],
"types": [
"hammerjs",
"jasmine"
],
"paths": {
"@shared/*": [
"../base-ui/*"
]
}
}
}
这是非常简单的内容constants.ts
export class BaseServerConstants {
public static readonly LOGOUT_URL = 'server/logout';
public static readonly LOGIN_URL = 'server/login';
}
这是我们如何导入/使用它
import { BaseServerConstants } from '@shared/constants/constants';
...
performLogout(): void {
window.location.href = BaseServerConstants.LOGOUT_URL;
}
只要我们使用ng build
or编写或构建项目,这就像一个魅力ng build --prod
。
但是ng serve
在 IntelliJ 或 VS Code 的控制台中使用总是会导致相同的错误消息,上面写着:
ERROR in /Users/developer/theproject/base-ui/constants/constants.ts
Module not found: Error: Can't resolve 'tslib' in '/Users/developer/theproject/base-ui/constants'
为什么会这样?为什么我能够使用此路径选项从位于 baseUrl 之外的简单 TS 文件中导入非常基本的类并在编码时应用它们。而且它们在构建最终应用程序时也不会导致编译器错误!之后应用程序按预期工作。
但是当这段代码应该在 NodeJS 中本地运行时,它不会编译?!
我必须设置其他选项吗?
非常感谢任何帮助或提示!