简体   繁体   English

Angular 2 在两个组件之间传递数据

[英]Angular 2 pass data between two components

I want to pass data between two components but my problem is:我想在两个组件之间传递数据,但我的问题是:

I have two components, lets suppose one is 'main' and another 'modal-dialog'.我有两个组件,假设一个是“主要”,另一个是“模态对话框”。

In my main I want to open the modal-dialog and get the data from my modal-dialog without leaving my main component在我的主要内容中,我想打开模态对话框并从我的模态对话框中获取数据而不离开我的主要组件

I know how to use @Input but I can't see a way to use that in my app我知道如何使用 @Input 但我在我的应用程序中看不到使用它的方法

For example in my main.html, if I want to pass data from main to modal I would use例如在我的 main.html 中,如果我想将数据从 main 传递到 modal 我会使用

<modal-dialog [data]="data"> </modal-dialog>

But I want to do the inverse但我想做相反的事情

Something like that类似的东西

<modal-dialog /*get data from modal when event happens*/ > </modal-dialog> 

Modal-dialog will send a message for my main, for example, if I close it or click in some button. Modal-dialog 会为我的 main 发送一条消息,例如,如果我关闭它或单击某个按钮。

Look to @Output看看@Output

<modal-dialog [data]="data" (DialogEvent)="processEvent($event)"> </modal-dialog>

in ModalDialogComponent在 ModalDialogComponent 中

@Output()
public DialogEvent = new EventEmitter();

public methodWhichTriggers(){
   this.DialogEvent.emit({id: 1, type: "anything you need"})
}

In MainComponent you will need to have在 MainComponent 中,您需要有

public processEvent($event){
   console.log($event); //will print {id: 1, type: "anything you need"}
}

I suggest that best approach is that to use rxjs subject.我建议最好的方法是使用 rxjs 主题。 You can pass data between modal dialog component or whatever it is.您可以在模态对话框组件或其他任何组件之间传递数据。 Create new rxjs subject in your service like this像这样在您的服务中创建新的 rxjs 主题

import { Subject } from 'rxjs/Subject';
@Injectable()
export class MyService {

    myNewSubject = new Subject<any>();

    informDataChanges(passyourobject){
      this.myNewSubject.next(passyourobject);
  }

} }

When change happens in your component or you want to pass data to another component simply call this service function from your component and pass data as parameter to this function.当您的组件发生变化或者您想将数据传递给另一个组件时,只需从您的组件调用此服务函数并将数据作为参数传递给此函数。 You can do that with something like this你可以用这样的东西来做到这一点

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

    @Component({
      selector: 'app-some',
      templateUrl: './some.component.html',
      styleUrls: ['./some.component.css']
    })
    export class SomeComponent implements OnInit {

      constructor( private myService: MyService) { }

      someFunction(){
        this.myService.informLogout('somedata');//Passing data to service here
      }

  ngOnInit() {

  }

}

Now all you need to do is subscribe to that data in another component.现在您需要做的就是在另一个组件中订阅该数据。 IMPORTANT subject keeps on watches any changes to it, and data will be continuous stream and it will be automatically subscribed.重要主题会随时关注它的任何更改,数据将连续流,并会自动订阅。 So it is best to subscribe to the subject in the constructor, and changes will be immediately reflected in that component.所以最好在构造函数中订阅主题,更改会立即反映在该组件中。

You do it with some thing like this你用这样的东西来做

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

        @Component({
          selector: 'app-another',
          templateUrl: './another.component.html',
          styleUrls: ['./another.component.css']
        })
        export class AnotherComponent implements OnInit {

          constructor( private myService: MyService) {
            this.myService.myNewSubject.subscribe(data=>{
             console.log(data);
       }) 
}

This way you can pass data between any components easily.通过这种方式,您可以轻松地在任何组件之间传递数据。

有关组件之间的各种通信,请参阅链接。

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

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