在Angular 6中的ngInit中调用Toastr的ExpressionChangedAfterItHasBeenCheckedError

时间:2018-06-21 07:28:08

标签: angular angular6

注册后,用户将重定向到登录页面,URL中带有?welcome = 1参数,以便它可以显示欢迎消息。

在“登录”组件中,我有:

ngOnInit() {
    ... 
    if (this.route.snapshot.queryParams['welcome']) {
      this.toastr.success('Register Successful, please login');
    }
  }

我得到他的消息:

Toast_Host.ngfactory.js? [sm]:1 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'className: undefined'. Current value: 'className: toast-success toast'. It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook ?
    at viewDebugError (core.js:8445)
    at expressionChangedAfterItHasBeenCheckedError (core.js:8433)
    at checkBindingNoChanges (core.js:8535)
    at checkNoChangesNodeInline (core.js:11401)
    at checkNoChangesNode (core.js:11390)
    at debugCheckNoChangesNode (core.js:11997)
    at debugCheckRenderNodeFn (core.js:11951)
    at Object.eval [as updateRenderer] (Toast_Host.ngfactory.js? [sm]:1)
    at Object.debugUpdateRenderer [as updateRenderer] (core.js:11940)
    at checkNoChangesView (core.js:11291)

我不明白,我没有做错任何事情,也没有更改任何值,那么为什么会出错?

3 个答案:

答案 0 :(得分:2)

尝试添加超时:

Laradock on mac
PhpStorm
PHP 7.1.16
Xdebug 2.6.0

答案 1 :(得分:2)

将烤面包机呼叫放入 setTimeout() 中的 ngAfterViewInit() 内:

ngAfterViewInit() {
  if (this.route.snapshot.queryParams['welcome']) {
    setTimeout(() => {
      this.toastr.success('Register Successful, please login');
    }, 0);
  }
}

答案 2 :(得分:-1)

尝试ngAfterViewInit内部

class MyComponent implements AfterViewInit{
  ngAfterViewInit() {
     if (this.route.snapshot.queryParams['welcome']) {
       this.toastr.success('Register Successful, please login');
     }
  }
}