简体   繁体   中英

How to pass the data from one component to another

I need to pass an id of object from one component to another.

<ng-container matColumnDef="actions">
      <mat-header-cell *matHeaderCellDef></mat-header-cell>
      <mat-cell *matCellDef="let user">
         <button mat-icon-button (click)="Edit()"><mat-icon>edit</mat-icon></button>             
      </mat-cell>
</ng-container>

Edit() function opens a form(another component) in which I would like to use something like user.id value. How can I pass the data between components? Is it possible to initialize a value and export it to another component in html code? Or is there any another solution? I would like to add that all the data is provided by service, MongoDB database.

If you are passing data from parent component to your child component you should use @input .

In your case you want to pass data from one component to another which are in the same level. i would suggest you to go for shared service .

Look at this answer to get more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM