我的问题是,如何将项的数组作为数据绑定值传递?

在我的例子中,我通过@input()给我的Angular 组件提供了一个IValuearray.
但是,当我判断应该接收数组的@input()变量时,它只是打印出一个对象数组,而不是IValue数组,尽管我给它的类型是IValues.

Component (FoodListPage) with data:

export class FoodListPage implements OnInit {
  foods: IValues[]; // <-- My data to pass
  category: string;

  constructor(private router: Router) {
    this.category = this.router.getCurrentNavigation().extras.state.categoryName; 
    this.foods = this.router.getCurrentNavigation().extras.state.values;
    console.log("food-list-data: ", this.foods);
  }

  ngOnInit() {}
}

The html of the component FoodListPage

<ion-content>
  <app-header title="{{category}}"></app-header>
  <app-food-listing values="{{foods}}"></app-food-listing> <!-- Here I pass the value (array of IValues) -->
  <app-nav-button page="main-menu"></app-nav-button>
</ion-content>

FoodListingComponent that should receive the value (array of IValues)

export class FoodListingComponent implements OnInit {
  @Input() values: IValues[];

  constructor() { }

  ngOnInit() {
    console.log("food-listing-data: ", this.values); //Console log (actual)
  }
}

预期数据:(4)[{…},{…},{…},{…}]&lt;--都来自类型IValue
实际数据:[OBJECT OBJECT],[OBJECT OBJECT]&lt;--仅来自类型"Object"...

推荐答案

请判断下面的Stackblitz,您需要使用Angular 属性绑定[values]="foods"传递非基元类型,如数组和对象,然后才会保持该类型.

<ion-content>
  <app-header [title]="category"></app-header><!-- changed here -->
  <app-food-listing [values]="foods"></app-food-listing> <!-- changed here -->
  <app-nav-button page="main-menu"></app-nav-button>
</ion-content>

forked stackblitz

Javascript相关问答推荐

如何在react-Router-dom 6中Forking 路由?

更新Reduxstore 中的状态变量,导致整个应用程序出现不必要的重新渲染

除了在Angular 16中使用快照之外,什么是可行且更灵活的替代方案?

在时间轴完整日历中显示日期标题

我在这个黑暗模式按钮上做错了什么?

为什么我的includes声明需要整个字符串?

React Code不在装载上渲染数据,但在渲染上工作

react—router v6:路由没有路径

简单的PayPal按钮集成导致404错误

我怎么才能得到Kotlin的密文?

用JS从平面文件构建树形 struct 的JSON

如何将数组用作复合函数参数?

使用auth.js保护API路由的Next.JS,FETCH()不起作用

变量在导入到Vite中的另一个js文件时成为常量.

自定义确认组件未在vue.js的v菜单内打开

更新Redux存储中的对象数组

在表单集中保存更改时删除';禁用';

无法使用npm install安装react-dom、react和next

是否设置以JavaScript为背景的画布元素?

我的NavLink活动类在REACT-ROUTER-V6中出现问题