MatSnackBar位置错误并隐藏

Pac*_*los 3 typescript angular-material2 angular

我正在处理MatSnackBar的错误,我对显示对话框的位置有问题而且它没有自动隐藏.

在此输入图像描述

service.ts

facebookLogin() {
const provider = new firebase.auth.FacebookAuthProvider();
return this.oAuthLogin(provider);
}

private oAuthLogin(provider) {
return this.afAuth.auth.signInWithPopup(provider)
  .then((credential) => {
    this.pushUserData(credential.user)
    this.router.navigate(['/userProfile'])
  })
  .catch(error => {
    this.handleError(error);

  });
}

private handleError(error: Error) {
  console.error(error);
  this.snackBar.open(error.message, this.action, { duration: 1000 });
}
Run Code Online (Sandbox Code Playgroud)

component.ts

facebookLogin() {
  this.auth.facebookLogin()
}
Run Code Online (Sandbox Code Playgroud)

当我从同一个组件测试它时,一切正常:

openSnackBar() {
this.snackBar.open(this.message, this.action, {
  duration: 500,
});
}
Run Code Online (Sandbox Code Playgroud)

Pac*_*los 7

我通过以下方式解决了它,添加了ngZone:

import { Injectable, NgZone } from '@angular/core';

constructor( public snackBar: MatSnackBar, private zone: NgZone )

private handleError(error: Error) {
console.error(error);
  this.zone.run(() => {
    this.snackBar.open(error.message, 'CERRAR', {
      duration: 5000,
    });
  });
}
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢。已经在谷歌上搜索了很多分钟,直到我找到了这个。为我节省了很多时间! (2认同)