简体   繁体   English

ngrx订户内的API调用

[英]API call inside ngrx subscriber

Are there any drawbacks / considerations to be made with making API calls inside subscribers? 在订户内部进行API调用是否有任何缺点/考虑因素? Or is there a preferred approach to this problem? 还是有解决此问题的首选方法? An example: 一个例子:

this.store.select('person').subscribe(person => {
    this.http.get(`/tasks/${person.id}`)
        .map(res => res.json())
        .subscribe(tasks => {
            // Dispatch tasks.update
        });
});

Any thoughts would be appreciated. 任何想法将不胜感激。

Tom 汤姆

I don't think that calling an HTTP method like that is a good idea. 我认为调用这样的HTTP方法不是一个好主意。

For example : 例如 :
- Your person is updated -您的person已更新
- The HTTP request is fired -触发了HTTP请求
- You dispatch an action to save the changes into the store -您分派动作将更改保存到存储中
- As the store is updated, you'll have another HTTP request launched -随着商店的更新,您将启动另一个HTTP请求
- Etc, etc -等

You'll end up with a loop and DDOS on your backend. 您最终将在您的后端出现一个循环和DDOS。

Instead of that, you should rather dispatch an action FETCH_PERSON , catch it with an Effect and once you get the response in the effect, dispatch another action according to the response (FETCH_PERSON_SUCCESS or FETCH_PERSON_ERROR for example). 取而代之的是,您应该分派一个动作FETCH_PERSON ,并用一个效果捕获它,一旦您获得了该效果的响应,就根据响应分派另一个动作(例如,FETCH_PERSON_SUCCESS或FETCH_PERSON_ERROR)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM