1

我正在使用ngc编译器提前编译我的 Angular 2 应用程序。在我的应用程序的一部分中,我使用了默认情况下 TypeScript 未涵盖的 Notification API。当我运行时ngc,它抱怨:

c:\xampp\htdocs\project\_dev>"node_modules/.bin/ngc" -p tsconfig.aot.json
Error: Error at c:/xampp/htdocs/project/_dev/src/app/shared/notify.service.ts:34:17: Cannot find name 'Notification'.
Error at c:/xampp/htdocs/project/_dev/src/app/shared/notify.service.ts:35:17: Cannot find name 'Notification'.
Error at c:/xampp/htdocs/project/_dev/src/app/shared/notify.service.ts:35:61: Cannot find name 'NotificationPermission'.
Error at c:/xampp/htdocs/project/_dev/src/app/shared/notify.service.ts:56:36: Cannot find name 'Notification'.
    at check (c:\xampp\htdocs\project\_dev\node_modules\@angular\tsc-wrapped\src\tsc.js:31:15)
    at Tsc.typeCheck (c:\xampp\htdocs\project\_dev\node_modules\@angular\tsc-wrapped\src\tsc.js:86:9)
    at c:\xampp\htdocs\project\_dev\node_modules\@angular\tsc-wrapped\src\main.js:33:23
    at process._tickCallback (internal/process/next_tick.js:103:7)
    at Module.runMain (module.js:606:11)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
Compilation failed

我有一个用于 window.Notification 对象及其方法的自定义 TS 定义文件,但是我如何告诉 ngc 编译器该文件位于何处?

我的 tsconfig.json 文件:

{
    "compilerOptions": {
        "target": "es5",
        "module": "es2015",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": ["es2015", "dom"],
        "noImplicitAny": false,
        "suppressImplicitAnyIndexErrors": true,
        "skipLibCheck": true,
        "outDir": "./compiled"
    },

    "files": [
        "./src/app/app.module.ts",
        "./src/main.aot.ts"
    ],

    "angularCompilerOptions": {
        "genDir": "aot",
        "skipMetadataEmit": true
    }
}
4

1 回答 1

2

弄清楚了:

  • custom_typings在我的项目的根目录中创建了一个文件夹;
  • 用我的自定义 TS 定义创建了一个名为的子文件夹notificationindex.d.ts
  • compilerOptions在我的 tsconfig 文件中添加了以下内容

"typeRoots": [
    "./custom_typings",
    "./node_modules/@types"
]

编译器会自动拾取它,一切都很好。

于 2016-12-18T09:49:19.907 回答