我写了一个用于从API加载类别项目的效果,但我的效果被无限期地调用。我该怎么做:
我注意到类别已正确加载,但请求无限期执行。
下面我输入代码:
category.effect.ts
@Injectable()
export class CategoryEffects {
@Effect()
getAll$: Observable<Action> = this.actions$
.ofType(category.LOAD)
.startWith(new category.LoadAction([]))
.switchMap(() => {
return this._categoriesService.getAllCategories()
.map(categories => new category.LoadAction(categories))
.catch(() => of(new category.LoadAction([])));
});
constructor(private actions$: Actions, private _categoriesService: CategoriesService) {
console.log('CategoryEffects constructor')
}
}
categories.service.ts
@Injectable()
export class CategoriesService {
private API_PATH = 'https://private-xxx.apiary-mock.com/categories';
constructor(private http: Http) {}
getAllCategories(): Observable<Category[]> {
return this.http.get(`${this.API_PATH}`)
.map(res => res.json() || []);
}
}
app.module.ts
@NgModule({
//...
imports: [
//...
EffectsModule.run(CategoryEffects)
],
//...
})