【发布时间】:2020-03-19 02:51:22
【问题描述】:
我正在尝试使用ngrx 做一个简单的口袋妖怪应用程序。我正在使用 lodash 库来做一个键值数组。我在填充 MatTableDataSource 所需的几乎所有步骤中都取得了成功,但我不知道如何为此提供正确的类型。所以我需要接收我的数据并用它填充我的dataSource.data,但我收到此错误:“类型'{ [key:string]:any;}'的参数不可分配给'any []'类型的参数。类型 '{ [key: string]: any; }' 缺少类型 'any[]' 中的以下属性:length、pop、push、concat 和另外 26 个”。因为这个,我的表没有渲染。如何为我的 MatTableDataSource 提供正确的类型或解决此问题?
问候
听到响应并将值设置为 MatTableDataSource 的我的 Observable 'pokemon.component.ts'
public readonly pokemonsSubscription = this.store.pipe(select(fromPokemons.pokemons)).subscribe(pokemons => {
if (!pokemons || pokemons.length === 0) {
return;
}
//the error is below
this.dataSource.data = new MatTableDataSource(pokemons);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});
这是我的口袋妖怪存储'pokemon.store.ts'
export const selectPokemonsState = (appState: AppState) => appState.Pokemons;
export const pokemons = createSelector(selectPokemonsState, (pokemonsState: PokemonsState) => pokemonsState.pokemons);
export const isLoading = createSelector(selectPokemonsState, (pokemonsState: PokemonsState) => pokemonsState.isLoading);
export const initial = createSelector(selectPokemonsState, (pokemonsState: PokemonsState) => pokemonsState.initial);
export const final = createSelector(selectPokemonsState, (pokemonsState: PokemonsState) => pokemonsState.final);
这些是我的动作'pokemons.actions.ts'
export enum Action {
PokemonsLoad = '[Pokemons] Load',
PokemonsLoadSuccess = '[Pokemons] Load Success',
PokemonsLoadError = '[Pokemons] Load Error',
PokemonLoadByQuantity = '[Pokemon] Load By Quantity',
PokemonLoadByQuantitySuccess = '[Pokemon] Load By Quantity Success',
PokemonLoadByQuantityError = '[Pokemon] Load By Quantity Error',
}
export const PokemonsLoad = createAction(Action.PokemonsLoad);
export const PokemonsLoadSuccess = createAction(Action.PokemonsLoadSuccess, props<{ payload: Array<any> }>());
export const PokemonsLoadError = createAction(Action.PokemonsLoadError, props<{ payload: any }>());
export const PokemonLoadByQuantity = createAction(Action.PokemonLoadByQuantity, props<{ payload: { initial: number, final: number }}>());
export const PokemonLoadByQuantitySuccess = createAction(Action.PokemonLoadByQuantitySuccess, props<{ payload: Array<any> }>());
export const PokemonLoadByQuantityError = createAction(Action.PokemonsLoadError, props<{ payload: any }>());
我的减速器'pokemons.reducer.ts'
export interface PokemonsState {
pokemons: { [key: string]: any };
isLoading: boolean;
initial: number;
final: number;
quantityOfAllPokemons: number;
}
export const pokemonsInitialState: PokemonsState = {
pokemons: {},
isLoading: false,
initial: 1,
final: 24,
quantityOfAllPokemons: undefined,
};
const pokemonsReducer = createReducer(
pokemonsInitialState,
on(pokemonsActions.PokemonsLoadSuccess, (state, { payload }) => (
{
...state,
pokemons: keyBy(PokemonNumber.pokemonNumber(payload), 'id'),
isLoading: false,
quantityOfAllPokemons: payload.length
}
)
),
on(pokemonsActions.PokemonLoadByQuantitySuccess, (state, { payload }) => (
{
...state,
pokemons: keyBy( Object.values(state.pokemons).concat(payload), 'id'),
isLoading: false,
initial: PokemonNumber.nextSearch(state.final, state.quantityOfAllPokemons, 1),
final: PokemonNumber.nextSearch(state.final, state.quantityOfAllPokemons, 12)
}
)
),
on(pokemonsActions.PokemonsLoad, (state) => (
{
...state,
isLoading: true
}
)
),
on(pokemonsActions.PokemonLoadByQuantity, (state) => (
{
...state,
isLoading: true
}
)
),
);
export function reducer(pokemonState: PokemonsState | undefined, action: Action) {
return pokemonsReducer(pokemonState, action);
}
export const pokemonsFeatureKey = 'Pokemons';
效果'pokemons.effects.ts'
@Injectable()
export class PokemonsEffects {
loadAllPokemons$ = createEffect(() => this.actions$.pipe(
ofType(Action.PokemonsLoad),
switchMap((payload) => {
return this.pokemonsService.getAllPokemons()
.pipe(
map(pokemons => (PokemonsLoadSuccess({ payload: pokemons }))),
catchError((msg) => of(PokemonsLoadError({ payload: msg }))),
);
})
)
);
loadPokemonByQuantity$ = createEffect(() => this.actions$.pipe(
ofType(Action.PokemonLoadByQuantity),
switchMap((payload) => {
return this.pokemonsService.loadPokemonByQuantity(payload['payload']['initial'], payload['payload']['final'])
.pipe(
map(pokemons => (PokemonLoadByQuantitySuccess({ payload: pokemons }))),
catchError((msg) => of(PokemonLoadByQuantityError({ payload: msg }))),
);
})
)
);
success$ = createEffect(() => this.actions$.pipe(
ofType<{ type: string, payload: any }>(
// Action.PokemonsLoadSuccess,
// Action.PokemonLoadByQuantitySuccess,
),
tap(({ type, payload }) => { window.alert('Sucesso'); })
), { dispatch: false });
error$ = createEffect(() => this.actions$.pipe(
ofType<{ type: string, payload: string }>(
Action.PokemonsLoadError,
Action.PokemonLoadByQuantityError,
),
tap(({ type, payload }) => { window.alert('Erro'); }),
), { dispatch: false });
constructor(private actions$: Actions, private pokemonsService: PokemonsService) { }
}
我关于这个错误的堆栈闪电在下面
【问题讨论】:
-
我们在 stackblitz 中看不到您的代码。使用您的 stackblitz 编辑器链接更改它
-
对不起,伙计。但是现在我已经更改了上面的链接。
标签: javascript angular typescript angular-material lodash