ngrx效果不编译

发布于 2025-02-07 18:12:53 字数 2564 浏览 1 评论 0原文

我创建了一个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:
Errors

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2025-02-14 18:12:53

您还必须从地图

NetworkStatusActions.fetchNetworkStatusSuccess({
  online: networkStatus,
}).   

和Catherror中返回动作

return of(
  NetworkStatusActions.fetchNetworkStatusFail({
     errorMsg: 'Unable to fetch current incident',
  })
)

you must have to return action from map

NetworkStatusActions.fetchNetworkStatusSuccess({
  online: networkStatus,
}).   

and in catchError also

return of(
  NetworkStatusActions.fetchNetworkStatusFail({
     errorMsg: 'Unable to fetch current incident',
  })
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文