So I have a base component and in it I have an observable inside its constructor to get a value once it is received inside another component that will validate the user data. However when trying to get this value in the child class the data arrives as undefined. Below the code I'm having problems:
My observable:
@Injectable()
export class ObservableUsuario implements ObservableUtil {
subject = new Subject<any>();
setUsuario(variable: any) {
this.subject.next({ text: variable });
}
clearUsuario() {
this.subject.next();
}
getUsuario(): Observable<any> {
return this.subject.asObservable();
}
Here observable has set:
getValue = (refresh: boolean = true) => cacheable(
() => this.http.get(`${environment.apiEndpoint}/api/Get/Value`, headers()).map(res => {
this.observableUser.setUsuario(res.json());
return res.json();
}),
);
Base component:
//more code
perfilLotado: any;
constructor(
public observableUser: ObservableUsuario
) {
this.observableUser.getUsuario().subscribe(
perfil => this.perfilLotado = perfil.text.lotacaoDTO[0].perfilDTO[0].nome);
}
//more code
Child component:
export class BaseListComponent extends BaseComponent {
NgOnInit(){}
validAction(situacao: number): any {
//Here appearer like undefined...
console.log(this.perfilLotado);
if (this.sistema.situacaoRestric[situacao] != undefined) {
this.showMsg('warn', 'Mensagem de Alerta', this.sistema.situacaoRestric[situacao]);
return false;
} else if (this.sistema.situacaoRestric[situacao] == undefined && this.sistema.perfilRestrict[this.perfilLotado] != undefined) {
this.showMsg('warn', 'Mensagem de Alerta', this.sistema.perfilRestrict[this.perfilLotado]);
return false;
} else if (this.sistema.situacaoRestric[situacao] == undefined && this.sistema.perfilRestrict[situacao] == undefined) {
return true;
}
}
In other words, my problem is loading the value that is passed by my observable to a child component class. NOTE: From what I understand it is the last component to load, how can I make it load before the other components of the screen?