我正在尝试使用ngrx做一个简单的口袋妖怪应用程序。我正在使用lodash库做一个键值数组。我已经成功地在几乎所有的步骤,我需要填写我的MatTableDataSource,但我的事情,我不知道如何给这个正确的类型。所以我需要接收我的数据,并填写我的www.example.com,但我收到这个错误:dataSource.data字符串]:任意;}"不能赋给类型为" any []"的参数。类型为"{[ key:字符串]:任意;"}"缺少类型" any []"的以下属性:length,pop,push,concat,还有26个"。因为这个,我的表没有呈现。如何给我的mattabledatasource正确的类型或者修复这个问题? }' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more". Because this, my table is not rendered. How to give the correct type to my MatTableDataSource or fix this issue?
问候
我的观察对象听到响应并将值设置为MatTableDataSource '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;
});
这是我的口袋妖怪存储"口袋妖怪. 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);
这些是我的动作"口袋妖怪.动作. 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 }>());
我的还原器"口袋妖怪.还原器. 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';
特效"宠物小精灵.特效. 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
2条答案
按热度按时间cetgtptt1#
该错误指示您正在尝试将对象分配给数组类型的属性。在下面的行中:
MatTableDataSource()
需要一个数组,但pockemons
似乎不是正确的对象数组。例如,您可以通过执行console.log(Array.isArray(pockemons));
来验证这一点。一旦你确认
pockemons
不是一个对象数组,你就可以把这个对象变成对象数组(或者单独提问)。您还应该将上面的行更改为(删除
.data
):仔细检查一下stackblitz,似乎当从存储中检索
pokemons
时,尽管它是一系列对象,但它不是作为对象数组进入的。mec1mxoz2#
我正在使用lodash库创建键值数组
你根本没有创建数组,它实际上是一个对象的对象,示例如下:
我不明白为什么你真的需要这样做,根据你的表是如何设置的,它需要一个数组,就像你从API中得到的一样。无论如何,你使用
pokemonNumber
来创建你的口袋妖怪的id,你用它来获取PokemonLoadByQuantitySuccess
中每个口袋妖怪的单独数据。所以我会这么做:
好吧,那么当为每个口袋妖怪获取数据时,您使用的是
concat
,这在这里是不合适的,因为您只是在连接数组。您需要的是根据id将数据附加到特定的口袋妖怪。这可能做得更干净,但至少这样做是有效的:最后,与其他答案一样,将
data
从:这似乎是工作。
你可能想调整你商店里的
loading
属性,当为口袋妖怪获取所有单独的数据时,当前图像出现延迟(至少对我来说)。