简体   繁体   English

角度:将数据从组件发送到另一个组件

[英]Angular: Send data from component to another component

I'm learning Angular, I have these components: 我正在学习Angular,我具有以下组件:

MainComponent.ts MainComponent.ts

import {Component} from 'angular2/core';
import {UsersTableComponent} from './UsersTableComponent';
import {UserAddComponent} from './UserAddComponent';

@Component({
    selector: 'main',
    templateUrl: '../app/views/mainView.html',
    directives: [UsersTableComponent, UserAddComponent]
})

export class MainComponent {
    constructor() {}
}

UserAddComponent.ts UserAddComponent.ts

import {Component} from 'angular2/core';
import {GenderPipe} from '../pipes/GenderPipe';

@Component({
    selector: 'userAdd',
    templateUrl: '../app/views/userAdd.html',
    pipes: [GenderPipe]
})

export class UserAddComponent {
    constructor() {}

    addUser() {
        alert(1);
    }
}

UserTableComponent UserTableComponent

import {Component} from 'angular2/core';
import {UserServices} from '../services/UserServices';
import {User} from '../classes/User';
import {GenderPipe} from '../pipes/GenderPipe';

@Component({
    selector: 'usersTable',
    templateUrl: '../app/views/usersTable.html',
    pipes: [GenderPipe]
})

export class UsersTableComponent {
    users: Array<User>

    constructor(UserServices: UserServices) {
        this.users = UserServices.getUsers();
    }
}

on the UserAddComponent on addUser method I need to read some values from the template and update users though UserServices . 在addUser方法上的UserAddComponent上,我需要从模板读取一些值,并通过UserServices更新用户。 When it is updated, I need to call a method from the UserTableComponent in order to refresh a table with the data added to the users. 更新后,我需要从UserTableComponent调用一个方法,以刷新带有添加到用户的数据的表。

How can I call a method in one component from another component? 如何从另一个组件中调用一个组件中的方法?

Have the parent of both components hold the list of users in a property and pass it to both children components using @Input , that way both will react whenever the list changes: 让两个组件的父组件将users列表保存在一个属性中,并使用@Input将其传递给两个子组件,这样,只要列表发生更改,两个组件都将做出反应:

https://angular.io/docs/ts/latest/api/core/Input-var.html https://angular.io/docs/ts/latest/api/core/Input-var.html

Alternative you can throw an event with @Output : 或者,您可以使用@Output抛出事件:

https://angular.io/docs/ts/latest/api/core/Output-var.html https://angular.io/docs/ts/latest/api/core/Output-var.html

You could add an event (user added) in the user service. 您可以在用户服务中添加事件(已添加用户)。 When the response is received from the user service (add an element), it can call the emit method of the corresponding event emitter. 从用户服务(添加元素)接收到响应后,它可以调用相应事件发射器的emit方法。 the UserTableComponent component can register on this event using the subscribe method to be notified when the event is fired. 所述UserTableComponent组件可以使用所述注册该事件subscribe方法中,当事件被触发时得到通知。

Another approach would be to trigger this 'user added' event from the UserAddComponent component itself: 另一种方法是从UserAddComponent组件本身触发此“用户添加”事件:

@Component({
  (...)
})
export class UserAddComponent {
  @Output() userAdded;

  constructor(private service:UserService) {
  }

  addUser() {
    // user is linked to a form for example
    this.service.addUser(this.user).subscribe(
      (addedUserData) => {
        this.userAdded.emit(addedUserData);
      }
  }
}

The other component (the table one) can use the event like this: 另一个组件(表一)可以使用如下事件:

<add-user (user-added)="refreshTable()"></add-user>

Hope it helps you, Thierry 希望对您有帮助,蒂埃里

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

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