2017-04-11 181 views
0

我正在学习如何为推送器通知实施通知服务。我基本上想跟踪推送者的通知。目前,我能够从推杆通道收到推送通知,但是当我点击它后,它消失/消失。事实上,我想在列表中显示推送者在我的应用程序中发出的通知,以便我可以跟踪它们。通知应该列在event.html页面上。 问题:通知服务如何使用推送功能来实现我上面解释的内容?Angular 2推送器的通知服务

通知服务

export class NotificationService { 
    private _notifications = new Subject<Notification>(); 

    public noteAdded = this._notifications.asObservable(); 

    public add(notification: Notification) { 
     this._notifications.next(notification); 
} 

商品通知模型

export class Notification{ 
    constructor(
     public type: string = '', 
     public message: string = '') {} 
} 

通知

@Component({ 
    selector: 'notifications', 
    template:` 
    <div class="notifications"> 
     <div (click)="hide(note)" class="{{ note.type }}" 
       *ngFor="let note of _notes"> 
      {{ note.message }} 
     </div> 
    </div> 
    `, 
    providers:[NotificationService] 
}) 

export class NotificationComponent{ 
    private _notes: Notification[]; 

    constructor(private _notifications: NotificationService) { 
     this._notes = new Array<Notification>(); 

     _notifications.noteAdded.subscribe(note => { 
      this._notes.push(note); 
      setTimeout(() => { this.hide.bind(this)(note) }, 3000); 
     }); 
    } 

    private hide(note) { 
     let index = this._notes.indexOf(note); 

     if (index >= 0) { 
      this._notes.splice(index, 1); 
     } 
    } 
} 

event.html

<notifications></notifications> 

event.ts

@Component({ 
    selector: 'pusher', 
    templateUrl: 'event.html', 
    providers:[HttpService, NotificationService] 
}) 
export class pusherComponent{ 
    constructor(private httpService:HttpService, private router:Router, private elementRef:ElementRef, private _notes:NotificationService) {} 

    ngAfterViewInit(){ 
     var s = document.createElement("script"); 
     s.type = "text/javascript"; 
     s.src ="src/app/js/pusher.js"; 
     this.elementRef.nativeElement.appendChild(s); 
    } 
} 
+2

你可以降低你的问题的长度3通过删除空白;-) –

+0

@GünterZöchbauer,我只是,并希望这是好的;-) – XamarinDevil

+0

我没有看到任何改变与白色空间相关。 –

回答