简体   繁体   English

组件之间的Angular 2 Pass数据

[英]Angular 2 Pass data between components

Here is the thing. 这是东西。 I have two components where data needs to be exchanged. 我有两个组件需要交换数据。 Let's say Component 1 and Component 2. 假设组件1和组件2

In Component 1 I have variable graph and method loadGraph 在组件1中,我具有可变图和方法loadGraph

 graph: any = new joint.dia.Graph;

 loadGraph(objectName, objectName2, objectCol1, objectCol2) {
    this.graph.fromJSON({... })
 }

In Component 2 I am calling that method 在组件2中,我正在调用该方法

this.edit.loadGraph(objectName, objectName2, objectCol1, objectCol2);

Now when I go back to Component 1 and try to log graph variable, its not set to anything. 现在,当我回到组件1并尝试记录图形变量时,它没有设置为任何值。 Which means Component 2 didn't pass any data to Component 1. 这意味着组件2没有将任何数据传递给组件1。

I am using Provider to communicate with components. 我正在使用提供程序与组件进行通信。 In Component 2 I have this: 在组件2中,我有这个:

import {Component1} from '....something';

@Component({
  selector: '...',
  templateUrl: '...',
  styleUrls: ['...'],
  providers: [Component1]
})

export class Component2 implements OnInit {

  constructor(private edit: Component1) {

  };
}

I probably need Child - Parent component as my solution? 我可能需要Child-Parent组件作为解决方案? If that's the case, how would my two components look? 如果是这样,我的两个组件看起来如何?

You need to provide the provider on a common parent of the components that need to communicate. 您需要在需要通信的组件的公共父节点上提供提供程序。 If you add it to providers of each component, each component instance will get it's own service instance and then communication won't work because they need to use a shared instance. 如果将其添加到每个组件的提供程序中,则每个组件实例将获得其自己的服务实例,然后通信将无法进行,因为它们需要使用共享实例。

Alternatively you can provide the service in @NgModule(...) . 或者,您可以在@NgModule(...)提供服务。 This way a single instance is shared with the whole application. 这样,单个实例便与整个应用程序共享。

@Gunter is right but i feel if your are making an enterprise level application and there will be many such components where you have to pass data between components , look at the redux architecture for angular either ngrx-store or ng2-redux. @Gunter是正确的,但是我觉得如果您要开发的是企业级应用程序,那么将会有很多这样的组件,您必须在这些组件之间传递数据,请查看redux体系结构中的ngrx-store或ng2-redux。

If you want to check how redux works please look at this repo in git hub has a very simple demo on ngrx store with angular. 如果要检查redux的工作方式,请在git hub中的这个仓库上查看有关ngrx store的一个非常简单的演示。

https://github.com/rahulrsingh09/AngularConcepts https://github.com/rahulrsingh09/AngularConcepts

... maybe still relevant for someone ... ...也许仍然与某人相关...

For me it looks like you want to call a method on Component1 (the child) triggered by an event (like a button click) on Component2 (the parent). 对我来说,您似乎想在Component2(父级)上的事件(如单击按钮)触发的情况下,在Component1(子级)上调用方法。 To be clear - the child component instance that "lives in the DOM", right? 要明确-“存在于DOM中”的子组件实例,对吗?

You can use @ViewChild for that. 您可以使用@ViewChild You will get a reference to Component1 and just call the function. 您将获得对Component1的引用,然后调用该函数。

If the parent/child thing is mixed up here this could be an option for the other way round. 如果父母/孩子的事情混在一起, 可能是另一种选择。

I would not provide components, just services . 我不会provide组件,而只会services Yes it is possible, you can mark any class with @Injectable (seems like it even works without), but it would never be the component instance "from the DOM". 是的,有可能,您可以使用@Injectable标记任何类(似乎即使没有也可以使用),但是它决不是“来自DOM”的组件实例。 It would be the instance of the "nearest" provided one. 这将是“最近”提供的一个实例。 If you provide at component level, your component will get a new instance your component created - and all your child components will get the same instance, if they use dependency injection . 如果您在组件级别提供,则组件将获得您的组件创建的新实例-如果所有子组件都使用依赖注入 ,则所有子组件将获得相同的实例。

如果您在Component1(parent) Component2(child)内部具有Component2(child) ,则可以通过以下方法从Component1调用Component2的方法:

`http://plnkr.co/edit/KK9v0DZSrD0L2tN5EXzD?p=preview`

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

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