gpt4 book ai didi

angular - 我的 NgRx 效果不工作,没有任何反应

转载 作者:搜寻专家 更新时间:2023-10-30 21:25:27 25 4
gpt4 key购买 nike

我的 NgRx 效果有问题。

应用程序正确地添加到商店,遗憾的是我的请求效果没有执行,即在启动添加新车时,将其添加到商店,仅此而已。问题是我的效果没有控制台日志,没有因为错误的 url 而导致的 http 错误,什么都没有。

我的应用代码:

reducer

import { Action } from '@ngrx/store';
import * as CarActions from './car.actions';
import { Car } from 'src/models/car.model';

const initialState: Car = {
brand: '',
model: ''
};

export function carReducer(state: Car[] = [initialState], action: CarActions.Actions) {

switch (action.type) {
case CarActions.ADD_CAR:
return [...state, action.payload];
case CarActions.ADD_CAR_FAIL:
return {
...state,
carError: action.payload,
};
default:
return state;
}
}

状态

import { Car } from './../../models/car.model';

export interface AppState {
readonly car: Car;
}

Action

import { Action } from '@ngrx/store';
import { Car } from './../../models/car.model';

export const ADD_CAR = '[CAR] Add';
export const ADD_CAR_FAIL = '[CAR] Fail';

export class AddCar implements Action {
readonly type = ADD_CAR;

constructor(public payload: Car) {}
}

export class AddCarFail implements Action {
readonly type = ADD_CAR_FAIL;

constructor(public payload: string) {}
}

export type Actions = AddCar | AddCarFail;

效果

import { Actions, Effect, ofType } from '@ngrx/effects';
import * as CarActions from './car.actions';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { switchMap, catchError, map, tap } from 'rxjs/operators';
import { of } from 'rxjs';

@Injectable()
export class CarEffects {
@Effect()
carAdd = this.actions$.pipe(
ofType(CarActions.ADD_CAR),
switchMap((carData: CarActions.AddCar) => {
console.log('true');
return this.http.post('http://myapi.com/api', { brand: carData.payload.brand, model: carData.payload.model }).pipe(map(resData => {
localStorage.setItem('test', 'asdasdasd');
}),
catchError(errorRes => {
console.log(errorRes);
const errorMessage = 'An unknown error occurred!';
if (!errorRes.error || !errorRes.error.error) {
return of(new CarActions.AddCarFail(errorMessage));
}

console.log(errorRes.error.error.message);
return of(new CarActions.AddCarFail(errorRes.error.error.message));
}));
})
);

constructor(
private actions$: Actions,
private http: HttpClient) { }
}

App.Module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { CarComponent } from './car/car.component';
import { StoreModule } from '@ngrx/store';
import { carReducer } from './car/car.reducer';
import { HttpClientModule } from '@angular/common/http';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';


@NgModule({
declarations: [
AppComponent,
CarComponent
],
imports: [
BrowserModule,
StoreModule.forRoot({ car: carReducer }),
HttpClientModule,
StoreDevtoolsModule.instrument({
maxAge: 5
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

最重要的是,我的代码是否以良好的方式使用了 NgRx?

最佳答案

你没有给你的模块添加 Effect

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { CarComponent } from './car/car.component';
import { StoreModule } from '@ngrx/store';
import { carReducer } from './car/car.reducer';
import { HttpClientModule } from '@angular/common/http';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { EffectsModule } from '@ngrx/effects';
import { CarEffects } from './car/car.effects';


@NgModule({
declarations: [
AppComponent,
CarComponent
],
imports: [
BrowserModule,
StoreModule.forRoot({ car: carReducer }),
HttpClientModule,
EffectsModule.forRoot([CarEffects]),
StoreDevtoolsModule.instrument({
maxAge: 5
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

关于angular - 我的 NgRx 效果不工作,没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57270283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com