简体   繁体   English

Angular 2事件发射器

[英]Angular 2 event emitter

I got this class : 我上了这堂课:

export class Project {
  $key: string;
  file: File;
  name: string;
  title: string;
  cat: string;
  url: string;
  progress: number;
  createdAt: Date = new Date();

  constructor(file: File) {
    this.file = file;
  }
}

I have upload component where I upload all of this information to my database/storage and it works fine. 我有上载组件,可以在其中将所有这些信息上载到我的数据库/存储中,并且工作正常。

Then I am showing all of the Projects in home.component like this : 然后,我在home.component中显示所有项目,如下所示:

Upload.Service : Upload.Service:

 getUploads() {
    this.uploads = this.db.list(`profile/${this.auth.userId}/project`).snapshotChanges().map((actions) => {
      return actions.map((a) => {
        const data = a.payload.val();
        this.showVisualContent(data.url, data.name);
        const $key = a.payload.key;
        const $ref = a.payload.ref;
        return { $key, ...data, $ref };
      });
    });
    return this.uploads;
  }

Home.Component : Home.Component:

 uploads: Observable<Project[]>;

ngOnInit() {
    this.uploads = this.navSrv.getUploads();
    }

Home.html : Home.html:

 <div *ngFor="let project of uploads | async" class="responsive-width">
  <mat-card-title class="project-card-title">{{project.name}}</mat-card-title>
</div>

In this way I am showing all projects in home.component. 这样,我将在home.component中显示所有项目。 What I want is : 我想要的是:

  • I click on one of the projects in my home.component. 我单击home.component中的项目之一。
  • Go to child component 转到子组件
  • See only clicked project information (not all projects). 仅查看单击的项目信息(并非所有项目)。

I know just a little about event emitters (maybe I need to use them), but I don't really know how to get that project on which I click and show it in child component. 我对事件发射器只了解一点(也许我需要使用它们),但是我真的不知道如何获得单击该项目并将其显示在子组件中的项目。 How To do that ? 怎么做 ?

getOneProject() { //and pass it to another component

}

You don't need EventEmmiter for this type of problem. 对于这种类型的问题,您不需要EventEmmiter。 EventEmmiters are used when you want to send some data from Child Component to Parent Component, not the other way around. 当您要将某些数据从子组件发送到父组件而不是相反时,可以使用EventEmmiters。

As i understood, you want to click on element and redirect to component with only that particular project data. 据我了解,您想单击元素并仅使用特定项目数据重定向到组件。 For this type of solution, you will need to have a route (for example /projectComponent) and when you click on you get redirected (using routerLink) to that route with project data like in this example: 对于这种类型的解决方案,您将需要一个路由(例如/ projectComponent),并且当您单击时,您将使用项目数据重定向(使用routerLink)到该路由,如以下示例所示:

<div *ngFor="let project of uploads | async" class="responsive-width">
    <mat-card-title class="project-card-title" [routerLink]="['./projectComponent', project]"> {{project.name}}</mat-card-title>
</div>

Hope it helps! 希望能帮助到你!

If the Project component is a child of the Home component, you don't need an event emitter. 如果Project组件是Home组件的子组件,则不需要事件发射器。 Just use the @Input() decorator in the parent's template to pass all the data you require in the child. 只需在父级模板中使用@Input()装饰器,即可在子级中传递所需的所有数据。 You can have a look at the official Angular documentation regarding passing data from parent to child with input binding. 您可以查看有关使用输入绑定将数据从父级传递到子级的官方Angular文档

Events can't be passed down from a parent to a child, in your case; 在您的情况下,无法将事件从父母传递给孩子; your better of using a service. 您最好使用服务。

Essentially, you'll want to create a component out of your project and iterate through that, then set a click event in the html to call a function which sets some data in the service based on the clicked project. 本质上,您将需要从项目中创建一个组件并对其进行迭代,然后在html中设置一个click事件以调用一个函数,该函数根据单击的项目在服务中设置一些数据。

Then all you'll need to do is pull that info down from the service into your child component. 然后,您所需要做的就是将该信息从服务中拉入子组件。

I've sudo coded the main part of the solution: 我已经对解决方案的主要部分进行了sudo编码:

export class projectHandlerService {
    public projectInfo: any;


    setProjectInfo(info: any) {
        this.projectInfo = info;
    }
}

@Component({//stuff here})
export class Project {
    $key: string;
    file: File;
    name: string;
    title: string;
    cat: string;
    url: string;
    progress: number;
    createdAt: Date = new Date();

    constructor(file: File, private projectHandler: projectHandlerService) {
      this.file = file;
    }

    onClick() {
        this.projectHandler.setProjectInfo(//whatever info you want to pass)
    }
  }

Basically the Project (child) component should have an input property: 基本上, Project (子)组件应具有输入属性:

import {Component, Input, OnInit} from '@angular/core';

...

export class ProjectComponent implements OnInit {

  @Input("project") project: Project;
  ...
}

And then your loop should bind to this input property in the Home component template: 然后,您的循环应绑定到Home组件模板中的此输入属性:

<div *ngFor="let project of uploads | async" class="responsive-width">
  <mat-card-title class="project-card-title" [project]=project></mat-card-title>
</div>

This way you will be able to pass down the project property and render it in the child component. 这样,您将能够传递project属性并将其呈现在子组件中。

In your specific case, you do not need to emit an event with an event emitter, because this is used in case you want to propagate data from the child component to its parent. 在您的特定情况下,您不需要通过事件发射器来发射事件,因为在您要将数据从子组件传播到其父组件的情况下,可以使用该事件。

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

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