注册后,用户将重定向到登录页面,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)


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

最佳答案

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

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

08-28 12:54