使用@ angular / material处理全局警报

时间:2017-09-15 15:06:14

标签: angular typescript angular-material angular-material2 angular-services

我打算如何处理@angular/material中的提醒?

import { Injectable } from '@angular/core';
import { MdSnackBar, MdSnackBarConfig } from '@angular/material';

@Injectable()
export class AlertsService {
  alerts: string[];

  constructor(private snackBar: MdSnackBar) {
    this.alerts = [];
  }

  public add(alert: string, action?: string, config?: MdSnackBarConfig) {
    this.alerts.push(alert);
    // console.warn('AlertsService::alerts =', this.alerts, ';');
    this.snackBar.open(alert, action, config);
  }
}

然后我有使用它的模块,如:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SomeComp1Component } from './some-comp1.component';
import { AlertsService } from '../alerts/alerts.service';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [SomeComp1Component],
  exports: [SomeComp1Component],
  providers: [AlertsService]
})
export class SomeComp1Module { }

https://github.com/AlecTaylor/material-alerts-ng

这是处理警报的最佳做法吗?

0 个答案:

没有答案