如何使用Angular 2 Alert(Toaster)设置超时警报消息

时间:2019-01-30 09:18:09

标签: angular

我正在按照教程http://jasonwatmore.com/post/2017/06/25/angular-2-4-alert-toaster-notifications发出警报消息,但是设置警报消息超时时遇到问题。我想在4秒钟后自动关闭警报。

这是我的alert.service.ts

private subject = new Subject<any>();
private alerts= false;

constructor(private router: Router) {
    // clear alert message on route change
    router.events.subscribe(event => {
        if (event instanceof NavigationStart) {
            if (this.alerts) {
                // only keep for a single location change
                this.alerts= false;
            } else {
                // clear alert
                this.subject.next();
            }
        }
    });
}

private alertMessage(message: string, alerting= false, type: string) {
    this.alerts = alerting;
    window.scrollTo(0, 0);
    this.subject.next({ type: type, msg: message });
}

public success(message: string, alerting = false) {
    this.alertMessage(message, alerting, 'success');

    setTimeout(() => {
         this.subject.next();
    }, 3000);
}

0 个答案:

没有答案