Ich versuche, eine grundlegende angular 2 App zu schreiben, die die neue Version von RxJS -> "rxjs" verwendet: "5.0.0-Beta.6".RxJS. Next() scheitert still in Angular 2 App
Ich habe die Anweisungen von der cookbook gefolgt, versucht, einen Benachrichtigungsdienst zu machen, der irgendein Teil meiner App anrufen könnte, um eine Nachricht anzuzeigen.
Das Problem, das ich habe, ist, dass, wenn ich .next()
anrufen, um die nächste Benachrichtigung hinzuzufügen, wird dies nicht vom Abonnement abgeholt. Die Zeile this.displayMessage(notification);
läuft nach einem Anruf auf newNotification
nicht. Ich habe den BehaviourSubject-Typ zu meinem Code hinzugefügt (im Gegensatz zu dem im Tutorial verwendeten Betreff) und festgestellt, dass der Anfangswert von der Subskription abgeholt wurde - der this.displayMessage(notification);
wurde erfolgreich bei der Initialisierung aufgerufen. Das lässt mich denken, dass es etwas damit zu tun hat, wie/wo ich .next() in der NotificationService
Klasse anrufe.
Hier sind die entsprechenden Klassen:
NotificationService:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Notification } from '../notification/notification';
@Injectable()
export class NotificationService {
// Observable string sources
private notificationSource = new BehaviorSubject<Notification>(new Notification({message:"test", priority:-1}));
notifications$ = this.notificationSource.asObservable();
newNotification(message: string, priority: number) {
this.notificationSource.next(new Notification({ message, priority }));
}
}
MessageComponent:
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Notification } from '../notification/notification';
import { NotificationService } from '../notification.service/notification.service';
import {MdIcon, MdIconRegistry} from '@angular2-material/icon';
import { Subscription } from 'rxjs/Subscription';
@Component({
selector: 'message-container',
styleUrls: ['./app/message/message.component.css'],
templateUrl: './app/message/message.component.html',
directives: [MdIcon],
providers: [NotificationService, MdIconRegistry]
})
export class MessageComponent implements OnDestroy, OnInit {
notification: Notification;
subscription: Subscription;
constructor(
private notificationService: NotificationService) {
this.notificationService = notificationService;
}
ngOnInit() {
this.subscription = this.notificationService.notifications$.subscribe(
notification => {
this.displayMessage(notification);
}, err => console.log(err),() => console.log("completed: "));
}
displayMessage(notification: Notification) {
this.notification = notification;
window.setTimeout(() => { this.notification = null }, 3000);
}
ngOnDestroy() {
// prevent memory leak when component destroyed
this.subscription.unsubscribe();
}
}
Wenn jemand irgendwelche Ideen über andere Dinge muss versuchen, das wäre toll . Vielen Dank
Edit: voll Repo hier: https://github.com/sandwichsudo/sentry-material/tree/notifications/src/app
Vielen Dank für die Antwort - Ich habe die Repo URL für den richtigen Zweig https://github.com/sandwichsudo/sentry-material/tree/notifications/src/app aktualisiert. Ich überprüfe, was Sie vorschlagen, obwohl ich zuversichtlich bin, dass ich es nirgendwohin eintrage, aber die Symptome klingen richtig. –
GitHub findet einfach nichts. Vielleicht muss es zuerst Ihr Repository indizieren. Haben Sie überprüft, ob Sie den Service mehrmals bereitstellen? –
Ich habe es mir angeschaut und verwende den NotificationService nur in Providern. Hier ist die Datei mit Bootstrapping: https://github.com/sandwichsudo/sentry-material/blob/notifications/src/main.ts und der Benachrichtigungsdienst wird nicht angezeigt. Ich bin nicht sicher, warum die Suche es nicht findet, es ist sicher in dieser Datei: https://github.com/sandwichsudo/sentry-material/blob/notifications/src/app/notification.service/notification.service .ts –