【问题标题】:Angular2 Notifications chaining with router navigateAngular2通知与路由器导航链接
【发布时间】:2016-07-17 09:14:51
【问题描述】:

我有一个简单的申请表,提交时会导航到另一个页面。 我想在单击提交按钮时显示成功消息。

以下代码有效,但提交后,它立即重定向到其他页面,该页面仅显示一秒钟的通知。 我无法将通知添加到目标页面,因为它有多个入口点。

onSubmit(form) {
this._service.success('Test', 'Test');  // this is from angular2-notificaitons library 
this.apartmentService.postRequests(this.data).then(_=>this.router.navigate(['Requests']));   //navigates to requests page which has multiple entry points
this.data = {};
}

知道如何实现这一目标吗?

【问题讨论】:

  • 我不知道通知库。当通知关闭或自动隐藏时,它如何允许得到通知? this._service.success('Test', 'Test'); 返回什么?
  • 我正在使用来自库github.com/flauc/angular2-notifications/blob/master/docs/…的这份文档
  • 您能否检查this._service.getEmitter().subscribe(event => console.log('event', event)) 是否发出任何可用于获取通知被删除通知的事件?
  • 该属性不存在..error TS2339: Property 'getEmitter' does not exist on type 'NotificationsService'.
  • 对不起,应该是getChangeEmitter()github.com/flauc/angular2-notifications/blob/…

标签: node.js angular angular2-routing angular2-services


【解决方案1】:

您在哪里注册了 NotificationsService 的提供程序。我认为问题在于您没有在应用程序的根目录中注册它。

尝试将 NotificationsService 和 SimpleNotificationsComponent 指令移动到您的 app.component。

   @Component({
        selector: "app",
        directives: [SimpleNotificationsComponent],
        providers: [NotificationsService, PushNotificationsService],
        template: `
           <simple-notifications [options]="options" (onCreate)="onCreate($event)" (onDestroy)="onDestroy($event)">
           </simple-notifications>
        `
   })

【讨论】:

  • 我在我将使用通知的组件中注册了它。它工作正常,只是链接将其重定向到另一个没有此服务的组件的方式。
  • 这就是问题所在。您应该在顶级组件中注册它,以便您导航到的组件知道通知。
猜你喜欢
  • 2018-06-08
  • 1970-01-01
  • 2016-11-14
  • 2016-12-22
  • 2019-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-20
相关资源
最近更新 更多