简体   繁体   English

如何从一个组件到另一个组件获取价值?

[英]How to get value from one component to another?

I have a small project working and i need to pass from one component to another text ( this information is passed from an array ).I should be able to click on a row and select that value so that i can then later on edit its parameters, which worked when i have the two .ts files in one component but as soon as i split them out then i cant figure it out as i am having both components on the screen the same time as seen below... later on the array will be stored on MONGODB. 我有一个正在执行的小项目,我需要从一个组件传递到另一文本(此信息从数组传递)。我应该能够单击一行并选择该值,以便以后可以编辑其参数,当我将两个.ts文件放在一个组件中时起作用,但是一旦我将它们拆分开,我就无法弄清楚了,因为我同时在屏幕上看到两个组件,如下所示……稍后在数组上将存储在MONGODB上。 would it make more sense to create a server.ts file for the array? 为该数组创建server.ts文件更有意义吗? if so how would i then connect every thing? 如果是这样,那我该如何连接每件事?

The first code sniped is from my child component called Listevent.component.: 截断的第一个代码来自我的子组件Listevent.component。

 import { Component, OnInit } from '@angular/core'; export interface PeriodicElement { date: number; club: string; name: number; flight: number; archers: number; scoring: number; awards: number; } const ELEMENT_DATA: PeriodicElement[] = [{ date: 1288323623006, club: 'D', name: 1.0079, flight: 1, archers: 1, scoring: 1, awards: 0 }, { date: 1288323623006, club: 'Helium', name: 4.0026, flight: 2, archers: 2, scoring: 2, awards: 0 }, { date: 1288323623006, club: 'Lithium', name: 6.941, flight: 3, archers: 3, scoring: 3, awards: 0 }, { date: 1288323623006, club: 'Beryllium', name: 9.0122, flight: 1, archers: 4, scoring: 4, awards: 0 }, { date: 1288323623005, club: 'Boron', name: 10.811, flight: 3, archers: 5, scoring: 5, awards: 0 }, { date: 1288323630066, club: 'Carbon', name: 12.0107, flight: 2, archers: 6, scoring: 6, awards: 0 }, { date: 1288323623006, club: 'Nitrogen', name: 14.0067, flight: 1, archers: 7, scoring: 7, awards: 0 }, { date: 1288323630068, club: 'Oxygen', name: 15.9994, flight: 4, archers: 8, scoring: 8, awards: 0 }, { date: 1288323630069, club: 'Fluorine', name: 18.9984, flight: 5, archers: 9, scoring: 9, awards: 0 }, { date: 11288323230060, club: 'Neon', name: 20.1797, flight: 2, archers: 10, scoring: 10, awards: 0 }, ]; @Component({ selector: 'app-listevent', templateUrl: './listevent.component.html', styleUrls: ['./listevent.component.scss'] }) export class ListeventComponent implements OnInit { displayedColumns: string[] = ['date', 'club', 'name', 'flight', 'archers', 'scoring', 'awards']; dataSource = ELEMENT_DATA; EventName: string; constructor() {} ngOnInit() {} onRowClicked(row) { console.log('Row clicked: ', row); this.EventName = row.name + ' - ( ' + row.club + ' ) '; } } 
 <style> table { width: 100%; } </style> <table mat-table [dataSource]="dataSource" class="mat-elevation-z8"> <!-- Position Column --> <ng-container matColumnDef="date"> <th mat-header-cell *matHeaderCellDef> <h2><b>Date</b></h2> </th> <td mat-cell *matCellDef="let element"> {{element.date | date:'yyyy-MM-dd Z'}} </td> </ng-container> <!-- Name Column --> <ng-container matColumnDef="club"> <th mat-header-cell *matHeaderCellDef> <h2><b>Club</b></h2> </th> <td mat-cell *matCellDef="let element"> {{element.club}} </td> </ng-container> <!-- Weight Column --> <ng-container matColumnDef="name"> <th mat-header-cell *matHeaderCellDef> <h2><b>Name</b></h2> </th> <td mat-cell *matCellDef="let element"> {{element.name}} </td> </ng-container> <!-- Symbol Column --> <ng-container matColumnDef="flight"> <th mat-header-cell *matHeaderCellDef> <h2><b>Flight</b></h2> </th> <td mat-cell *matCellDef="let element"> {{element.flight}} </td> </ng-container> <ng-container matColumnDef="archers"> <th mat-header-cell *matHeaderCellDef> <h2><b>Archers</b></h2> </th> <td mat-cell *matCellDef="let element"> {{element.archers}} </td> </ng-container> <ng-container matColumnDef="scoring"> <th mat-header-cell *matHeaderCellDef> <h2><b>Scoring</b></h2> </th> <td mat-cell *matCellDef="let element"> {{element.scoring}} </td> </ng-container> <ng-container matColumnDef="awards"> <th mat-header-cell *matHeaderCellDef> <h2><b>Awards</b></h2> </th> <td mat-cell *matCellDef="let element"> {{element.awards}} </td> </ng-container> <tr style="background-color: lightsteelblue; font-weight: bold" mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;" (click)="onRowClicked(row)"></tr> </table> 

My second component is EventManagement.component.: 我的第二个组件是EventManagement.component。

 import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core'; import { ListeventComponent } from './listevent/listevent.component'; @Component({ selector: 'app-eventmanagement', templateUrl: './eventmanagement.component.html', styleUrls: ['./eventmanagement.component.scss'] }) export class EventmanagementComponent implements OnInit, AfterViewInit { constructor() {} EventName1: string; @ViewChild(ListeventComponent) child; ngOnInit() { this.EventName1 = 'test'; } ngAfterViewInit() { this.EventName1 = this.child.EventName; console.log(this.child.EventName); } } 
 <style> main { width: 90%; margin: auto; } .page mat-card-header { justify-content: center; } .Event mat-card-header { background-color: #00acc1; justify-content: left; } .Event mat-card-header a { margin: auto; } .eventbody mat-card-content a { margin: auto; } table { width: 100%; border: 1px solid black; } main mat-card { margin-top: 0.5rem; } </style> <main> <mat-card style="border-radius: 25px" class="Event"> <mat-card-header style="border-radius: 25px"> <a mat-raised-button color="accent"> Events </a> <a mat-raised-button color="primary"> Flights </a> <a mat-raised-button color="primary"> Archers </a> <a mat-raised-button color="primary"> Scoring Groups </a> <a mat-raised-button color="primary"> Manage </a> <a mat-raised-button color="primary"> Awards </a> <a mat-raised-button color="warn"> Question/Support </a> </mat-card-header> </mat-card> <mat-card style="border-radius: 25px" class="eventbody"> <mat-card-header style="border-radius:25px; background-color: lightsteelblue"> <h4>Event: [ {{EventName1}} ]</h4> </mat-card-header> <mat-card-content> <a mat-raised-button color="primary" style="border-radius:25px;"> Create </a> <a mat-raised-button color="primary" style="border-radius:25px;"> Edit </a> <a mat-raised-button color="primary" style="border-radius:25px;"> Clone </a> <a mat-raised-button color="warn" style="border-radius:25px;"> Cancel </a> </mat-card-content> </mat-card> <mat-card> <app-createevent></app-createevent> </mat-card> <mat-card style="border-radius: 25px"> <app-listevent></app-listevent> </mat-card> </main> 

Child to Parent: 孩子对父母:

ParentComponent: 为父级:

export class AppComponent implements AfterViewInit  {
  @ViewChild(ChildComponent) child;

  name = 'Angular';
  message = '';

  ngAfterViewInit() {
    this.message = this.child.message
  }
}

ChildComponent: ChildComponent:

export class ChildComponent  {
  @Input() name: string;
  message = 'Hey! I am in ChildComponent :)';
}

Here is the Stackblitz 这是Stackblitz

Parent to Child: 父母与子女:

Pass data to your child component from parent through template and @Input() . 通过模板和@Input()数据从父级传递到子级组件。

In the parent component: 在父组件中:

<app-listevent [data]="data"></app-listevent>

And get it in the child component.ts: 并将其放在子component.ts中:

@Input('data') data;

Do not forget to import the @Input() from Angular Core. 不要忘记从Angular Core导入@Input()

In your child component, make sure to use @Input , like so: 在子组件中,确保使用@Input ,如下所示:

Import: 进口:

import { Input } from '@angular/core';

Set it, above the constructor function: 在构造函数上方进行设置:

@Input() someVariable;

Use it, perhaps on ngOnInit : 使用它,也许在ngOnInit

if (this.someVariable === undefined) this.someVariable = 'default value';

Read more here . 在这里阅读更多。

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

相关问题 如何将价值从一个组件发送到另一个组件 - How to send value from one component, to another React:如何从一个组件获取输入值以在另一个组件中进行Ajax调用? - React : how to get input value from one component to make the ajax call in another component? 如何在没有导航的情况下从一个组件获取价值到另一个页面/组件? - How to get value from one component to another page/component without navigation? 反应:如何将结果从一个组件传递到另一组件 - React: How to get results from one component to another component 从一个组件中获取价值,并在另一个组件中使用它。 - Get value from one component and use it in another. 如何在react js中将值从一个组件传递到另一个组件 - how to pass value from one component to another in react js 如何在 React 中将动态更新的值从一个组件传递到另一个组件? - How to pass dynamically updated value from one component to another in React? 如何在 VueJS 中将值从一个子组件传递到另一个子组件? - How to pass value from one child component to another in VueJS? 如何在单击时从一个兄弟组件中增加一个兄弟组件中的值? - How to increment a value in one sibling component from another on click? 如何将 state 变量的值从一个组件传递到另一个组件? - How to pass value of state variable from one component to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM