属性'staticAlert'没有初始值设定项,并且在构造函数中没有明确赋值。


import { Component, OnInit, ViewChild } from '@angular/core';
import {Subject} from 'rxjs';
import {debounceTime} from 'rxjs/operators';
import {NgbAlert} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

staticAlertClose=false;
successMesage='';
private _success = new Subject<string>();

@ViewChild('staticAlert', {static: false}) staticAlert: NgbAlert;
@ViewChild('selfClosingAlert', {static: false}) selfClosingAlert: NgbAlert;

ngOnInit(): void {
//throw new Error('Method not implemented.');
setTimeout(() => this.staticAlert.close(), 20000);
this._success.subscribe(message => this.successMessage = message);
this._success.pipe(debounceTime(5000)).subscribe(() => {
if (this.selfClosingAlert) {
this.selfClosingAlert.close();
}
});
}
close(alert: Alert) {
this.alerts.splice(this.alerts.indexOf(alert), 1);
}
reset() {
this.alerts = Array.from(ALERTS);
}
title = 'blog';
}

enter code here

我已经启用了严格的flag来帮助捕捉问题,我正在处理这个问题。我在app.component.ts文件中编写了这段代码,并从https://ng-bootstrap.github.io/#/components/alert/examples对于自动关闭警报,例如

我的工作方式是添加一个默认值。出于某种原因,编译器不喜欢它不是从跳转中定义的,即使Angular文档没有从跳转中进行定义。

这对我有效:

@ViewChild('selfClosingAlert', { static: false }) selfClosingAlert: NgbAlert | undefined;

最新更新