【问题标题】:angular 5 noty and AOTangular 5 noty 和 AOT
【发布时间】: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'.

标签: angular noty


【解决方案1】:

我发现这是这样工作的:

/// <reference path="../../../../node_modules/noty/index.d.ts" />

import { Injectable } from '@angular/core';
import Noty = require('noty');

@Injectable()
export class NotificationService {
    private noty(options) {
        return new Noty(options).show();
    }
    ...
}

因为 noty 没有将 d.ts 放入 node_modules/@types/... 那么我想我必须手动引用它。

PS:在 tsling.json 中也要设置"no-reference": false 规则,否则会将引用标记为错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2018-06-28
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 2018-02-23
    • 2018-06-24
    相关资源
    最近更新 更多