我试图通过将类装饰器添加到提供程序并注入父服务来将子服务添加到父服务,但是在我重写装饰类的构造函数时,将父服务定义为未定义,甚至尝试使用反射元数据的defineMetada与没有成功,下面是代码段。
@Injectable({
providedIn: 'root'
})
export class ParentService {
constructor() {
}
}
导出类MyClass { constructor(){ console.log('称为MyClass'); } }
在这里,我正在尝试添加子服务
@ServiceConfig({
services: [MyClass]
})
@Injectable({
providedIn: AppModule
})
export class AppService extends ServiceProvider {
constructor(parent: ParentService) {
super(parent);
console.log('app service');
console.log(parent);
}
}
export class ServiceProvider {
constructor(private _parent: PService) {
console.log('base', this._parent); // <-- here getting parent is undefined
}
public init(services: any[]) {
console.log('init',services);
}
}
在这里尝试覆盖构造函数
export function ServiceConfig(parameter: any) {
return function (target: any) {
const original = target;
const decoratedConstructor: any = function (...args: any[]): void {
console.log('Before original', args);
original.apply(this, args);
console.log('After original');
};
decoratedConstructor.prototype = original.prototype;
// Copy static members too
Object.keys(original).forEach((name: string) => { decoratedConstructor[name] = (<any>original)[name]; });
return decoratedConstructor;
};
}