【发布时间】:2021-01-08 20:33:31
【问题描述】:
拥有
export class CrudService<T> {
constructor(...) {}
protected index$: Observable<T[]> = ...
protected delete(id: string) { ... }
protected update(id: string, obj: T) { ... }
protected create(obj: T) { ... }
}
具体的类如:
interface Message {
from: string;
to: string;
body: string;
}
export class MessageService extends CrudService<Message> {
constructor(...) { super(...); stuff() }
public index$ = super.index$; // Results in null
public index$; // Results in loss of type information (update: any)
}
如何在不修改实现的情况下以最少的样板简单地公开基本方法?
【问题讨论】:
-
什么方法?您似乎正在尝试访问一个您从未初始化过的道具 (
index$)。 -
对不起,方法或属性。并初始化
private index$: Observable<T[]> = ... -
也许你应该在你的基类上使用受保护的访问级别而不是私有的?
-
谢谢@НиколайГольцев,实际上这就是我正在尝试的,不知道它是如何在问题中成为私人的。修好了。
标签: typescript inheritance typescript-generics