我有一个Angular 6服务,它封装了HTTP请求:
@Injectable({providedIn: MyModule})
export class MyService {
constructor(private _http: HttpService) { }
public getData(): Observable<model.Data[]> {
return this._http.get('/my-service');
}
}
和@ngrx效果:
@Injectable()
export class MyEffects {
@Effect()
load$: Observable<Action> = this._actions$.pipe(
ofType<fromActions.Load>(fromActions.ActionTypes.Load),
mergeMap(action => {
return this._myService.getData();
}),
map(data => {
return new fromActions.LoadSuccess(data);
})
);
constructor(
private _actions$: Actions,
private _myService: MyService
) {}
}
效果与服务注册在同一模块中,从而产生循环依赖关系:模块->效果->服务->模块。
在效果中注入服务的最佳方法是什么?我还能使用{providedIn: MyModule}
语法吗?
答案 0 :(得分:1)
尝试使用喷油器
import {Injector} from '@angular/core';
_myService = injector.get(MyService);