我在metadata.ts
中有以下内容:
export interface InjectableDecorator {
(): any;
new (): Injectable;
}
export interface Injectable {
}
export declare const Injectable: InjectableDecorator;
然后,我将Injectable
导入到模块main.ts
中,然后像这样使用它:
import {Injectable} from "@angular/core";
@Injectable()
export class ComboService {}
我有两个问题:
Injectable
引用const Injectable
而不是export interface Injectable
?new @Injectable()
允许使用error TS1109: Expression expected.
执行功能,InjectableDecorator
为什么会报告错误new
?答案 0 :(得分:3)
接口Injectable和var(本例中为const)Injectable实际上是两个不同的实体,适用于不同的空间。您也可以说let number:number = 4;
当您将Injectable引用为值而不是类型时,TS引用const。
@
语法特定于将装饰器应用于目标。一个新的表达式期望......好吧......一个表达式。删除@
并致电new Injectable()
,它应该可以正常工作。