ngrx get value in function

泪湿孤枕 提交于 2021-02-11 03:33:51

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!