Angular编译器引发内部错误:未知标识符{} [英] Angular Compiler throws Internal error: unknown identifier {}

查看:48
本文介绍了Angular编译器引发内部错误:未知标识符{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样构建我的angular2服务

I am constructing my angular2 services like so

import { OpaqueToken, ClassProvider } from "@angular/core";

export interface IService {
  // ... interface declaration
}

export class Service implements IService {
  // ... implementation goes here
}

export const IService = new OpaqueToken(Service.name);

export const ServiceProvider: ClassProvider = {
    provide: IService,
    useClass: Service,
};

使用Angc 2.x.x的ngc可以很好地工作和编译但是,升级到angular 4.x.x之后,在组件提供程序"数组中使用ServiceProvider时,不能再对其进行ngc编译.(有趣的是,当提供者在ngModule中而不在组件中注册时,整个过程就起作用了.)

This has worked and compiled wonderfully using ngc with angular 2.x.x But after upgrading to angular 4.x.x, the ServiceProvider can no longer be ngc-compiled when used in a components 'providers' array. (Interestingly, the whole thing works when the provider is registered in an ngModule and NOT in a component.)

我在控制台上看到以下内容:

I get the following on my console:

events.js:160投掷者//未处理的错误"事件^错误:命令失败:npm运行ngc错误:内部错误:未知标识符{}

events.js:160 throw er; // Unhandled 'error' event ^ Error: Command failed: npm run ngc Error: Internal error: unknown identifier {}

有什么想法吗?我可能会将我的服务转换为@Injectable()而不使用该接口,但这似乎与使用服务接口的整个想法背道而驰:(

Any ideas what's going on? I might convert my service to @Injectable() and not use the interface but that seems to run counter to the whole idea of using service interfaces :(

F.Y.I:我正在使用TypeScript 2.2.2

F.Y.I: I am using TypeScript 2.2.2

非常感谢您的帮助!

推荐答案

不幸的是,用InjectionjectToken替换OpaqueToken并不能解决问题.但是,所做的是使用抽象类而不是接口注入令牌 .

Unfortunately, replacing OpaqueToken with InjectionToken did not solve the issue. What did however, was using abstract classes instead of interfaces and injection tokens.

根据此答案 Angular 2注入接口?这是一个常见的配方(也可以使用由角度小组本身提供)注入抽象类,并像接口一样使用它们.

According to this answer Angular 2 Injectable Interface? it is a common recipe (also used by the angular team itself) to inject abstract classes and use them like interfaces.

import { ClassProvider } from "@angular/core";

export abstract class IService {
  // ... interface declaration
}

export class Service implements IService {
  // ... implementation goes here
}

export const ServiceProvider: ClassProvider = {
    provide: IService,
    useClass: Service,
};

这篇关于Angular编译器引发内部错误:未知标识符{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆