【发布时间】:2018-09-22 14:05:50
【问题描述】:
我以这种方式使用角度为 5.2.9 的 noty (https://ned.im/noty):
import { Injectable } from '@angular/core';
import Noty = require('noty');
@Injectable()
export class NotificationService {
private noty(options) {
return new Noty(options).show();
}
...
}
并且工作正常。
但是当我使用 AOT (npm run build:aot) 构建时,我得到:
ERROR in src/app/sys/util/notification.service.ts(2,1): error TS1202:
Import assignment cannot be used when targeting ECMAScript modules.
Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
这是一个基于https://github.com/gdi2290/angular-starter的项目。
使用 import Noty from 'noty'; 时,AOT 构建很好,但运行 npm run server 时,我得到 ERROR TypeError: noty_1.default is not a constructor。
node_modules/noty/index.d.ts 开头:
declare module 'noty' {
export = Noty;
}
declare class Noty {
constructor(options?: Noty.Options);
...
我应该如何解决这个问题?
【问题讨论】:
-
它在错误中正确地说:
import * as Noty from 'noty'。然后尝试新的Noty.Noty(...),是的,奇怪的语法。 -
我尝试使用
npm run server,得到:ERROR in src/app/sys/util/notification.service.ts(24,21): error TS2339: Property 'Noty' does not exist on type 'typeof Noty'.。