简体   繁体   English

如何在 Angular 中将数据或值从一个组件传递到另一个组件

[英]How to Pass Data or values From One Component to another in Angular

Good day Community, Please i am working on an ecommerce project and i would like to pass the quantity entered in my cart component to my checkout component, and when the value of the quantity changes, i would like it to change in the checkout component also.美好的一天社区,请我正在做一个电子商务项目,我想将在我的购物车组件中输入的数量传递给我的结帐组件,当数量的值发生变化时,我希望它在结帐组件中也发生变化. I have searched for solutions but couldnt find any.我已经搜索了解决方案,但找不到任何解决方案。 Someone please assist me.有人请帮助我。 Thank you谢谢

This is my cart.component.ts这是我的 cart.component.ts

  trackByCartItems(index: number, item: any) {
    return item._id;
  }

  get cartItems() {
    return this.data.getCart();
  }


  get cartTotal() {
    let total = 0;
    this.cartItems.forEach((data, index) => {
      total += data['price'] * this.quantities[index];
    });
    return total;
  }

  removeProduct(index, product) {
    this.quantities.splice(index, 1);
    this.data.removeFromCart(product);
  }


and my cart.component.html和我的购物车.component.html

<div class="col-1 mt-5 mt-md-0 p-0 induc">
                <input type="number" class="form-control text-left boy"  [(ngModel)]="cart.quantities[i]">
              </div>
              

Thank you in advance先感谢您

You have a few options, as detailed in this link.您有几个选项,如此链接中所述。

You can use @input , if the relationship between the components is parent-child, or you can use a service.如果组件之间的关系是父子关系,则可以使用@input ,也可以使用服务。

I recommend reading up more on passing data between components, in this link.我建议在链接中阅读有关在组件之间传递数据的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将角度2中的数据从一个组件传递到另一个组件? - how to pass data from one component to another component in angular 2? 以角度 6 将数据从一个组件传递到另一个组件 - Pass data from one component to another in angular 6 将数据从一个组件传递到另一个 Angular 2 - Pass Data from One Component to Another Angular 2 以角度将数据从一个组件传递到另一个组件 - Pass data from one component to another in angular 将数据从一个Angular组件传递到另一个 - Pass data from one Angular component to another Angular如何将ngModel数据从一个组件传递到另一个组件? - Angular how to pass ngModel data from one component to another? 如何在Angular 5中将服务数据从一个组件传递到另一个组件 - How to pass service data from one to another component in Angular 5 如何将数据从一个组件传递到另一个 angular2 - How to Pass the data from one component to another angular2 如何以角度 9 将表数据从一个组件推送/传递到另一个组件? - How to push/pass table data from one component to another in angular 9? 如何在 Angular 中将数据从一个组件传递到另一个组件? - How do I pass data from one component to another in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM