我有一个调用服务的函数。
submitPost(value:any)
{
this._adminLogin.postAdminLogin(this.adminLoginmodel).subscribe(
data => {
this.responseStatus = data;
if(this.responseStatus.status == 1)
{
localStorage.setItem('admin_id', this.responseStatus.detail.id);
this._flashMessagesService.show(this.responseStatus.message, { cssClass: 'alert-success', timeout: 5000 });
top.location.href = 'admin/dashboard';
}
else
{
this._flashMessagesService.show(this.responseStatus.message, { cssClass: 'alert-danger', timeout: 2000 });
}
},
err => {
console.log(err)
},
() => {}
);
this.status = true;
}我关心的是这段代码:
if(this.responseStatus.status == 1)
{
localStorage.setItem('admin_id', this.responseStatus.detail.id);
this._flashMessagesService.show(this.responseStatus.message, { cssClass: 'alert-success', timeout: 5000 });
top.location.href = 'admin/dashboard';
}有没有办法在5000毫秒后flash消息消失后执行重定向操作?就像这样:
if(this.responseStatus.status == 1)
{
localStorage.setItem('admin_id', this.responseStatus.detail.id);
this._flashMessagesService.show(this.responseStatus.message, { cssClass: 'alert-success', timeout: {function(){ top.location.href = 'admin/dashboard'; }, 5000 });
}发布于 2018-04-06 20:42:49
下面的代码应该在消息消失后导航。您的flashMessage将显示5000毫秒,您的导航应在7000毫秒之后显示
if(this.responseStatus.status == 1)
{
localStorage.setItem('admin_id', this.responseStatus.detail.id);
this._flashMessagesService.show(this.responseStatus.message, { cssClass: 'alert-success', timeout: 5000 });
setTimeout(()=>{
top.location.href = 'admin/dashboard';
},7000);
}发布于 2018-04-06 20:43:40
我会通过让_flashMessagesService.show()返回一个可观察对象来做到这一点。
在_flashMessageService中,如下所示:
myObserver : Observer<any>
function show(){
// do stuff
return Observable.create(observer =>{
this.myObserver =observer;
});
}当您准备解析可观察对象时,您可以执行以下操作:
this.myObserver.next('Possibly some value here');
this.myObserver.complete();在您的调用代码中
this._flashMessagesService.show()
.subscribe(result=> { top.location.href = 'admin/dashboard'});}我一直在做类似的事情。
另一种可能更简单的替代方法是在主组件中使用Observable.timer:
Observable.timer(5000).subscribe(result=> { top.location.href = 'admin/dashboard'}); 我更喜欢第一种方式,因为它取决于flashMessage服务决定flash消息何时消失,而不是硬编码的时间值。
https://stackoverflow.com/questions/49692819
复制相似问题