ngrx效果不编译
我创建了一个NetworkStatus NGRX商店/状态,以及一个检查应用程序是否在线的服务。我还需要NGRX效果,该效应采用FetchNetworkStatus操作,并使用该服务用布尔来更新商店。但是,即使在使用该操作之前,我似乎只是无法正确处理,并且有很多错误。所有错误都与效果有关,因此我必须在那里做错了很多事情。我正在努力理解所有效果方法。
这是我的效果的代码:
@Injectable()
export class NetworkStatusEffects {
constructor(
private networkStatusService: NetworkStatusService,
private actions$: Actions
) {}
fetchNetworkStatus$ = createEffect(() =>
this.actions$.pipe(
ofType(NetworkStatusActions.fetchNetworkStatus),
mergeMap(() =>
this.networkStatusService.getNetworkStatus().pipe(
map((networkStatus) =>
NetworkStatusActions.fetchNetworkStatusSuccess({
online: networkStatus,
})
),
catchError(() =>
of(
NetworkStatusActions.fetchNetworkStatusFail({
errorMsg: 'Unable to fetch current incident',
})
)
)
)
)
)
);
}
这是服务:
@Injectable({
providedIn: 'root'
})
export class NetworkStatusService {
networkStatus = false;
networkStatus$: Subscription = Subscription.EMPTY;
async getNetworkStatus() {
this.networkStatus = navigator.onLine;
this.networkStatus$ = merge(
of(null),
fromEvent(window, 'online'),
fromEvent(window, 'offline')
)
.pipe(map(() => navigator.onLine))
.subscribe(status => {
this.networkStatus = status;
});
return this.networkStatus;
}
}
以及动作:
export const fetchNetworkStatus = createAction('[NetworkStatus] Fetch networkStatus');
export const fetchNetworkStatusSuccess = createAction('[NetworkStatus] Fetch networkStatus status success', props<{online: boolean}>());
export const fetchNetworkStatusFail = createAction('[NetworkStatus] Fetch networkStatus status fail', props<{errorMsg: string}>());
和还原器:
export interface NetworkStatusState {
online: boolean;
error: string;
}
export const initialState: NetworkStatusState = {
online: true,
error: ''
};
export const reducer = createReducer(
initialState,
on(NetworkStatusActions.fetchNetworkStatusSuccess, (state, {online}) => ({
...state,
online: online
}))
);
我在这里做错了什么?该服务只是返回布尔值,所以我认为这不是太复杂了,我认为我只是不明白。
I have created a NetworkStatus ngrx store/state and a service that checks if the app is online. I also need an ngrx effect that takes a fetchNetworkStatus action and uses the service to update the store with a boolean. However, i just can't seem to get it right and there are loads of errors, even before using the action. All the errors are connected to the effect, so i must have done a lot of things wrong there. I am struggling to understand all the effect methods.
Here are the errors i get in the terminal:
Here is my code for the effect:
@Injectable()
export class NetworkStatusEffects {
constructor(
private networkStatusService: NetworkStatusService,
private actions$: Actions
) {}
fetchNetworkStatus$ = createEffect(() =>
this.actions$.pipe(
ofType(NetworkStatusActions.fetchNetworkStatus),
mergeMap(() =>
this.networkStatusService.getNetworkStatus().pipe(
map((networkStatus) =>
NetworkStatusActions.fetchNetworkStatusSuccess({
online: networkStatus,
})
),
catchError(() =>
of(
NetworkStatusActions.fetchNetworkStatusFail({
errorMsg: 'Unable to fetch current incident',
})
)
)
)
)
)
);
}
And here is the service:
@Injectable({
providedIn: 'root'
})
export class NetworkStatusService {
networkStatus = false;
networkStatus$: Subscription = Subscription.EMPTY;
async getNetworkStatus() {
this.networkStatus = navigator.onLine;
this.networkStatus$ = merge(
of(null),
fromEvent(window, 'online'),
fromEvent(window, 'offline')
)
.pipe(map(() => navigator.onLine))
.subscribe(status => {
this.networkStatus = status;
});
return this.networkStatus;
}
}
as well as the actions:
export const fetchNetworkStatus = createAction('[NetworkStatus] Fetch networkStatus');
export const fetchNetworkStatusSuccess = createAction('[NetworkStatus] Fetch networkStatus status success', props<{online: boolean}>());
export const fetchNetworkStatusFail = createAction('[NetworkStatus] Fetch networkStatus status fail', props<{errorMsg: string}>());
and reducer:
export interface NetworkStatusState {
online: boolean;
error: string;
}
export const initialState: NetworkStatusState = {
online: true,
error: ''
};
export const reducer = createReducer(
initialState,
on(NetworkStatusActions.fetchNetworkStatusSuccess, (state, {online}) => ({
...state,
online: online
}))
);
What am i doing wrong here? The service just return a boolean, so it shouldn´t be too complicated to put in the store i think, i just don't understand how..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还必须从地图
和Catherror中返回动作
you must have to return action from map
and in catchError also