问题
I am unsure if this is the right thinking, but I just started using ngrx for a project and now everything is an observable. It works well when I use the async pipe in the component view, but I am struggeling when I need the value from the store in code. My current approach is to make a property and subscribe in the ngOnInit to the selector, so I can use the current value of the state in my functions. For example storing configration options (webservice address) and then using that inside a service.
@Injectable()
export class HttpService {
localOptions$ = this.store.pipe(select(fromOptions.selectLocalOptionsState));
options: fromLocalOptions.State;
constructor(
private store: Store<fromOptions.State>,
) {
this.localOptions$.subscribe(localOptions => this.options = localOptions);
}
getEntities (fParams): Observable<any> {
console.log(this.options.webServiceUrl);
}
}
This feels weird, so my question is: Is this the way to do it, or am I missing something, but everywhere I read that there is no easy way to get "the current value from observable" which I think I already understand why.
来源:https://stackoverflow.com/questions/49555931/ngrx-get-value-in-function