从angular 9中的基类继承服务



我有下面的服务,我正试图用基类继承我的服务,这样我就可以把所有常量选项数据放在基类中

服务类

import { BaseNotification } from './baseNotification';
declare let toastr: any;
@Injectable({
providedIn: 'root'
})
export class NotificationService extends BaseNotification
{
constructor() {
super();
toastr.options =  this.options;
}
success(message: string, title?: string): void {
toastr.success(message, title);
}

BaseNotification类

export class BaseNotification {
options = {};
constructor(){
this.options = {
"closeButton": true,
"positionClass": "toast-top-right",
"preventDuplicates": false,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "1000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
}
}

但它不识别基类。我不想创建一个单独的子服务。我做错什么了吗?

toast可能会在基类中声明,notificationService实现逻辑。

@Injectable({providedIn:'root'}(导出类NotificationService扩展BaseNotification{

构造函数(toast:ToastrService({super(烤面包机(
}

成功(消息,标题({return super.tother.success(消息、标题(;}

}

我不确定是否在类外声明let语句,但如果你想将超类常量初始化为子类,你可以创建实例变量并初始化为它,如下所示:

@Injectable({
providedIn: 'root'
})
export class NotificationService extends BaseNotification
{
public toastr: any;
constructor() {
super();
toastr =  this.options;
}
-----
------
}

以下是堆叠式示例

最新更新