Ich habe dies mehrmals in meiner App getan. Es ist einfach, es sollte funktionieren ... Aber dieses Mal nicht.Angular 2: Observable/Subscription nicht triggern
Mein Problem:
ich eine Methode in einem Dienst von einer Komponente A nenne, meine Komponente B gezeichnet ist, aber nicht reagiert noch nichts erhalten. subscribe()
löst nicht aus!
navigations elements.service.ts
@Injectable()
export class NavigationElementsService {
updateIBOsNavigation$: Observable<any>;
private updateIBOsNavigationSubject = new Subject<any>();
constructor() {
this.updateIBOsNavigation$ = this.updateIBOsNavigationSubject.asObservable();
}
updateIBOsNavigation(navigationData) {
log.d('updateIBOsNavigation', JSON.stringify(navigationData));
this.updateIBOsNavigationSubject.next(navigationData);
}
}
IboDetailsGeneral
Komponente
export class IboDetailsGeneral implements OnInit, OnDestroy {
id: string;
private sub: any;
constructor(private route: ActivatedRoute, private iboService: IBOsService, private navigationService: NavigationElementsService) {
this.sub = this.route.params.subscribe(params => {
this.id = params['id'];
console.log('CALLING updateIBOsNavigation FUNCTION');
this.navigationService.updateIBOsNavigation(this.id);
});
}
ngOnInit() {
console.log('CALLING updateIBOsNavigation FUNCTION AGAIN');
this.navigationService.updateIBOsNavigation('test');
}
ngOnDestroy() {
this.sub.unsubscribe();
}
}
Diese Komponente löst die Methode des Service: updateIBOsNavigation
.
IBOsNavigationElement
Komponente
export class IBOsNavigationElement implements OnInit {
private id: string;
constructor(private navigationService: NavigationElementsService) {
this.navigationService.updateIBOsNavigation$.subscribe((navigationData) => {
log.d('I received this Navigation Data:', JSON.stringify(navigationData));
this.id = navigationData;
}
);
}
ngOnInit() {
}
}
Diese Komponente gezeichnet wird, sollte sie die Daten aufgeführt und erhalten ...
Lasst uns DI aussortieren: berücksichtigen, dass IboDetailsGeneral
in einer unteren Schicht auf der App-Struktur, so IboDetailsGeneral
ist Kind von IBOsNavigationElement
. Diese
ist, warum ich NavigationElementsService
in IBOsNavigationElement
hinzufügen 's-Modul:
NavigationModule
ist IBOsNavigationElement
' s Modul
@NgModule({
imports: [
// A lot of stuff
],
declarations: [
// A lot of stuff
IBOsNavigationElement
],
exports: [
// A lot of stuff
],
providers: [
NavigationElementsService
]
})
Console:
CALLING updateIBOsNavigation FUNKTION
updateIBOsNavigation "95"
CALLING updateIBOsNavigation Funktion erneut
updateIBOsNavigation "test"
Diese Konsole Ergebnisse mir sagt, dass:
- Die Methode aufgerufen wird, so dass keine Anbieterfehler. Die Kommunikation mit dem Service ist OK.
Meine Tests:
- Ich habe versucht, in
IBOsNavigationElement
eine zufällige Methode aufrufen (der Hörer), und die Kommunikation mit der Service ist gut. - Der einzige Ort, wo
NavigationElementsService
zuproviders
hinzugefügt wird, ist inNavigationModule
, so gibt es nur eine Instanz der Service-Recht? Dann erklärte das Problem in dem folgenden Link findet nicht statt: Angular 2 observable subscription not triggering
Ich bin wirklich leid für meine ‚Wand des Textes‘ aber an dieser Stelle Ich bin ein bisschen verzweifelt.
Jede Hilfe ist appreated, danke!
Update 1:
Nach der ersten Antwort, die ich einige Dinge ausprobiert haben ...
ReplaySubject
Verwendung:
@Injectable()
export class NavigationElementsService {
public updateIBOsNavigation$ = new ReplaySubject();
updateIBOsNavigation(navigationData) {
log.d('updateIBOsNavigation', JSON.stringify(navigationData));
this.updateIBOsNavigation$.next(navigationData);
}
}
Ergebnis: Wenn updateIBOsNavigation()
Aufruf, subscribe()
ist nach wie vor nicht auslösend.
Mit BehaviorSubject
:
@Injectable()
export class NavigationElementsService {
updateIBOsNavigationSubject = new BehaviorSubject<any>('');
updateIBOsNavigation$ = this.updateIBOsNavigationSubject.asObservable();
updateIBOsNavigation(navigationData) {
log.d('updateIBOsNavigation', JSON.stringify(navigationData));
this.updateIBOsNavigationSubject.next(navigationData);
}
}
Ergebnis: Es tritt subscribe()
bei der Initialisierung, aber wenn ich updateIBOsNavigation()
nennen, subscribe()
ist noch nicht ausgelöst wird.
Mit BehaviorSubject
v2:
Ich habe versucht, diesen Ansatz: behaviourSubject in angular2 , how it works and how to use it
@Injectable()
export class NavigationElementsService {
public updateIBOsNavigation$: Subject<string> = new BehaviorSubject<string>(null);
updateIBOsNavigation(navigationData) {
log.d('updateIBOsNavigation', JSON.stringify(navigationData));
this.updateIBOsNavigation$.next(navigationData);
}
}
Ergebnis: Wie in früheren Anwendung von BehaviorSubject
.
Update 2:
Weitere Proben von verzweifelten Versuche, nach der ganzen Bahn Erforschung ...
Mit BehaviorSubject
v3:
@Injectable()
export class NavigationElementsService {
updateIBOsNavigation$: Observable<any>;
updateIBOsNavigationSubject = <BehaviorSubject<any>> new BehaviorSubject([]);
constructor() {
this.updateIBOsNavigation$ = this.updateIBOsNavigationSubject.asObservable();
}
updateIBOsNavigation(navigationData) {
log.d('updateIBOsNavigation()', JSON.stringify(navigationData));
this.updateIBOsNavigationSubject.next(navigationData);
}
}
Ergebnis: Wie in früheren BehaviorSubject
Versuche ... Die Verzweiflung steigt ...
Update 3:
Nur für den Fall, ich wollte sicherstellen, dass NavigationElementsService
ist ein Singletons:
export class NavigationModule {
static forRoot() {
return {
ngModule: NavigationModule,
providers: [NavigationElementsService]
};
}
}
Und beim Import:
imports: [
NavigationModule.forRoot()
]
Ergebnis: Gleiches Problem wie immer, subscribe()
nicht auslösend, aber bei le Ich weiß, dass es eine Instanz von NavigationElementsService
gibt.
Ich füge Updates mit meinen verzweifelten Versuchen hinzu ... Bitte beenden Sie meine Folter! xD – SrAxi
Hallo, haben Sie dieses Problem jetzt gelöst? –
@BonesandTeeth Ja! Vielen Dank! : D – SrAxi