Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在angular 2中,如何在两个组件之间进行通信?_Angular - Fatal编程技术网

在angular 2中,如何在两个组件之间进行通信?

在angular 2中,如何在两个组件之间进行通信?,angular,Angular,假设两个组件使用相同的数据源(用于查看和编辑)。如果一个组件更改了数据,我需要立即将其反映到另一个组件上,反之亦然。现在有一种简单的方法可以做到这一点 您可以在angular中使用事件API 下面是我目前正在使用的示例 从“离子角度”导入{Events} 用法: constructor(public events: Events) { /*========================================================= = Keep this block i

假设两个组件使用相同的数据源(用于查看和编辑)。如果一个组件更改了数据,我需要立即将其反映到另一个组件上,反之亦然。

现在有一种简单的方法可以做到这一点

您可以在angular中使用事件API

下面是我目前正在使用的示例

从“离子角度”导入{Events}

用法:

constructor(public events: Events) {

/*=========================================================
=  Keep this block in any component you want to receive event response to            =
==========================================================*/

// Event Handlers
events.subscribe('menu:opened', () => {
  // your action here
  console.log('menu:opened');
});

events.subscribe('menu:closed', () => {
  // your action here
  console.log('menu:closed');
});

}

/*=====================================================
= Call these on respective events - I used them for Menu open/Close          =
======================================================*/


menuClosed() {
// Event Invoke
  this.events.publish('menu:closed', '');
}

menuOpened() {
// Event Invoke
  this.events.publish('menu:opened', '');
}
}

现在有一个简单的方法可以做到这一点

您可以在angular中使用事件API

下面是我目前正在使用的示例

从“离子角度”导入{Events}

用法:

constructor(public events: Events) {

/*=========================================================
=  Keep this block in any component you want to receive event response to            =
==========================================================*/

// Event Handlers
events.subscribe('menu:opened', () => {
  // your action here
  console.log('menu:opened');
});

events.subscribe('menu:closed', () => {
  // your action here
  console.log('menu:closed');
});

}

/*=====================================================
= Call these on respective events - I used them for Menu open/Close          =
======================================================*/


menuClosed() {
// Event Invoke
  this.events.publish('menu:closed', '');
}

menuOpened() {
// Event Invoke
  this.events.publish('menu:opened', '');
}
}

我得到了答案。我是通过不同视图中的ViewBinding加载数据源组件的。因此,每当我将数据注入组件时,它们都会接收到数据源的不同实例。当我将数据源放置在引导组件(主组件)的“绑定”数组中时,它开始将同一实例注入所有子组件。它们现在被弃用。我得到了答案。我是通过不同视图中的ViewBinding加载数据源组件的。因此,每当我将数据注入组件时,它们都会接收到数据源的不同实例。当我将数据源放入引导组件(主组件)的“绑定”数组中时,它开始将同一实例注入所有子组件。它们现在已被弃用。。