[英]angular rxjs Subject and subscribe not working
In my service I create an observable the I will use in another component 在我的服务中,我创建了一个可观察对象,将在另一个组件中使用
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { Router } from '@angular/router';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
@Injectable()
export class ApiHandlerService {
modalObject = new Subject<any>();
constructor(
private router: Router,
private http: HttpClient,
) {}
responseHandler(response) {
if (response.success) {
const obj = {
exception: false,
payload: response.payload
};
this.modalObject.next(obj);
return obj;
} else {
const obj = {
exception: true,
message: response.exception.message,
segNum: response.exception.seqNum
};
this.modalObject.next(obj);
return obj;
}
}
errorHandler(err) {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
this.router.navigate(['/app-login']);
}
}
}
}
then in my component I subscribe to the observable. 然后在我的组件中,我订阅了可观察的东西。
import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { ApiHandlerService } from 'app/services/api-handler/api-handler.service';
@Component({
selector: 'app-api-handler-modal',
templateUrl: './api-handler-modal.component.html'
})
export class ApiHandlerModalComponent implements OnInit, OnDestroy {
@ViewChild('autoShownModal') autoShownModal: ModalDirective;
isModalShown = false;
subscription: Subscription;
constructor(
private apiHandlerService: ApiHandlerService
) {}
ngOnInit() {
this.subscription = this.apiHandlerService.modalObject.subscribe(obj => console.log(obj));
}
ngOnDestroy() {
// this.subscription.unsubscribe();
}
showModal(obj?): void {
this.isModalShown = true;
}
hideModal(): void {
this.autoShownModal.hide();
}
onHidden(): void {
this.isModalShown = false;
}
}
so you can see that when I subscribe, I am just simply at this point trying to console.log the subscribe value. 因此您可以看到,当我订阅时,我只是在此时尝试console.log订阅值。 This seems to not be working and I am a bit too new at observables to figure what is going wrong? 这似乎无法正常工作,而且我在可观察指标上有点太新了,无法弄清楚到底出了什么问题?
In general, this code works 通常,此代码有效
ngOnInit() {
this.allSpecies = this.birdService.getAllSpeciesFromFirebase();
this.specieSub = this.allSpecies.subscribe(data => console.log(data));
}
Here, "allSpecies" is an observable and specieSub is a Subscription. 在这里,“ allSpecies”是可观察的,而specieSub是“订阅”。 The data will get logged as an array or wrapped object based on how service returns it. 数据将根据服务返回的方式记录为数组或包装对象。
Since a Subject is superclass of Observable, it should work as well.. Anyhow try downgrading to Observable itself and see if all code works fine... 由于Subject是Observable的超类,因此它也应正常工作。.无论如何,请尝试降级为Observable本身,并查看所有代码是否工作正常...
Post any updates or logs generated 发布任何更新或生成的日志
some reasons that may have caused this: 可能导致此问题的一些原因:
1.you have to add the service in the providers 1.您必须在提供商中添加服务
@Component({
selector: 'app-api-handler-modal',
templateUrl: './api-handler-modal.component.html',
providers:[ApiHandlerService]
})
when you do subscribe you should do service.methodInService().subscribe() in your case 当您订阅时,应根据情况进行service.methodInService()。subscribe()
this.apiHandlerService.responseHandler().subscribe(obj => console.log(obj)); this.apiHandlerService.responseHandler()。subscribe(obj => console.log(obj)); } }
constructor( public apiHandlerService: ApiHandlerService) 构造函数( 公共 apiHandlerService:ApiHandlerService)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.