我试图用离子2构建一个小购物车。我的问题是我不知道如何用我在特定数组中的元素填充我的提供者。 这是我的代码:
//cart.ts
myFunction(index:number){
this.cartItems.push({1:this.people[index].name.first, 2:this.people[index].qty});
this.cartService.add()
//don't know what to do
cart-service.ts
add(){
//I want to pass my cartItems array here to use it in another page
}
感谢您的帮助
答案 0 :(得分:0)
...如果我理解你的问题:
所以你的组件中有数组cartItems
,你可以用你的方法传递它:
this.cartService.add(this.cartItems) // add your array as parameter
然后在服务:
中let items = []; // declare an Array
add(items) {
this.items = items; // populate the array
}