【问题标题】:Property 'staticAlert' has no initializer and is not definitely assigned in the constructor.ts属性 'staticAlert' 没有初始化程序,也没有在 constructor.ts 中明确分配
【发布时间】:2021-07-03 20:10:39
【问题描述】:
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

我启用了严格标志来帮助发现问题,我遇到了这个问题。 我在 app.component.ts 文件中编写了这段代码,并从https://ng-bootstrap.github.io/#/components/alert/examples 获取了代码 自动关闭警报示例

【问题讨论】:

    标签: angular angular-bootstrap strict viewchild


    【解决方案1】:

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

    这对我有用:

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

    【讨论】:

      猜你喜欢
      • 2021-04-13
      • 2021-05-21
      • 2021-05-06
      • 2021-08-14
      • 2021-02-28
      • 2021-08-30
      • 2021-12-25
      相关资源
      最近更新 更多