当我使用firebase js sdk,创建一个angular项目,实现生命周期钩子(例如,afterviewinit),并在控制台上编写一些东西时,控制台消息再次无限重复。我希望该消息只会出现在控制台上一次。
感谢您对这个问题的解决
AppModule {
constructor(){
firebase.initializeApp(environment.firebase);
}
}
import { Component, DoCheck } from '@angular/core';
import { UserService } from ...;
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html'
})
export class NavbarComponent implements DoCheck {
public isCollapsed = true;
isLoggedIn = false;
constructor(public userService: UserService) {}
ngDoCheck(){ console.log("navbar docheck"); }
} 发布于 2019-07-10 22:13:57
据我所知,您在所有的生命周期钩子中都放了一个console.log()。
如果您在ngAfterContentChecked / ngAfterViewChecked中也这样做过,那么您必须记住,每次运行更改检测(应用程序状态更改)时,它都会不断地执行,如果您有一个console.log(),它将无限地出现
ngDoCheck是一个执行更改检测的回调方法,在默认的更改检测器运行后调用。同样,每次运行更改检测(应用程序状态更改)时,如果有一个console.log(),它将无限出现。如果希望它只运行一次,请使用ngOnInit、ngAfterContentInit或ngAfterViewInit
https://stackoverflow.com/questions/56972487
复制相似问题