代码之家  ›  专栏  ›  技术社区  ›  Paco Zevallos

Matsnakbar位置错误和隐藏

  •  3
  • Paco Zevallos  · 技术社区  · 6 年前

    我正在处理Matsnakbar的错误,我对对话框的显示位置有问题,而且它不会自动隐藏。

    enter image description here

    服务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 });
    }
    

    组成部分ts

    facebookLogin() {
      this.auth.facebookLogin()
    }
    

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

    openSnackBar() {
    this.snackBar.open(this.message, this.action, {
      duration: 500,
    });
    }
    
    3 回复  |  直到 6 年前
        1
  •  12
  •   Paco Zevallos    6 年前

    我用以下方法解决了这个问题,添加了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,
        });
      });
    }
    
        2
  •  0
  •   William Aguera    6 年前

    试试这个。

    this.snackBar.open(message, 'Close', { duration: 3500, verticalPosition: 'top', panelClass: 'snack-error'});
    
        3
  •  0
  •   daniekpo    6 年前

    你可以用

    private handleError(error: Error) {
        console.error(error);
        this.snackBar.open(error.message, this.action,
        { duration: 1000
          verticalPosition: 'top',
          horizontalPosition: 'center'
        });
    }
    

    您可以查看文档中的可用配置选项 here