首页 文章

Angular ngFor无法找到不同的支持对象'[object Object]'

提问于
浏览
2

我试图通过使用以下方法迭代RxJS存储状态的Observable:

ngOnInit() {
this.route.params
  .subscribe(
    (params: Params) => {
      this.index = +params['propertyId'];
      this.propertyState = this.store.select('propertyState');

      if (Number.isInteger(this.index)) {
        this.store.dispatch(new fromPropertyAction.SelectProperty(this.index));
      }
    }
  );

}

HTML:* ngFor =“let property of propertyState | async).properties; let i = index”(click)=“onSelectProperty(i)”

并尝试过:

ngOnInit() {
this.route.params
  .subscribe(
    (params: Params) => {
      this.index = +params['propertyId'];
      this.store.select('propertyState').subscribe(data => {
        this.properties = data.properties;
      })

      if (Number.isInteger(this.index)) {
        this.store.dispatch(new fromPropertyAction.SelectProperty(this.index));
      }
    }
  );

}

HTML:* ngFor =“let property of properties; let i = index”

但是这两种情况我都得到这个错误 .

找不到'对象'类型的不同支持对象'[object Object]' . NgFor仅支持绑定到诸如Arrays之类的Iterables .

此外,这里的store在类型的构造函数中声明:private store:Store

fromApp.AppState在app reducer中:

export interface AppState {
    propertyState: fromPropertyReducer.State
}

fromPropertyReducer.State在属性reducer中:

export interface State {
    properties: Property[];
    selectedProperty: Property;
    selectedPropertyIndex: number;
    openAllProperties: boolean;
    selectedPropertyExpenseIndex: number;
}

此外,最初的属性在html迭代数组中显示正常 . 但是在向property.expenses数组添加新开销的操作之后,是否会发生此错误 . 这就是我在reducer中添加新费用的方法:

case PropertyListActions.ADD_PROPERTY_EXPENSE:
        let property = state.properties[state.selectedPropertyIndex];

        const updatedExpenses = [
            ...property.expenses,
            action.payload
        ];

        const updatedProperty = {
            ...property,
            expenses: updatedExpenses
        };

        const updatedProperties = {
            ...state.properties,  
        };

        updatedProperties[state.selectedPropertyIndex] = updatedProperty;

        return {
            ...state,
            properties: updatedProperties    
        }

我看了别人的帖子,所以尝试了第二种方法,但没有奏效 . 请帮忙 . 谢谢 .

1 回答

  • 0

    谢谢你们每一个人的帮助!我只是想到你已经建议我的propertyState.properties需要是一个数组 . 由于在发出操作后发生错误,我发现错误在reducer中 . updatedProperties应该是一个数组,之前我把它作为一个对象 . 再次感谢你的帮助!我非常感谢 .

相关问题