【发布时间】:2018-09-19 13:11:50
【问题描述】:
我正在尝试创建自定义装饰器,它返回一个类的新实例,并且这个类必须从一个注入的服务创建,我不知道如何从装饰器函数访问它
自定义装饰器
export function Collection(endpoint: string) {
return function (constructor: any) {
const mainService = CollectionModule.injector.get(MainService);
return mainService.getCollection(endpoint);
};
}
我想从我尝试使用模块但injector 属性不存在的自定义装饰器中访问MainService!
服务
@Injectable()
export class MainService {
config;
getCollection(endpoint: string): Collection {
return new Collection(endpoint, this.config);
}
}
预期用途
export class AppComponent implements OnInit {
@Collection('posts') postsCollection;
ngOnInit() {
console.log(this.postsCollection);
}
}
更新
【问题讨论】:
-
@JonathanHamel 我已经检查过了,但是没有用
标签: angular typescript angular-decorator