Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.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
Javascript 订阅Angular2中的EventEmitter不工作_Javascript_Angular_Eventemitter - Fatal编程技术网

Javascript 订阅Angular2中的EventEmitter不工作

Javascript 订阅Angular2中的EventEmitter不工作,javascript,angular,eventemitter,Javascript,Angular,Eventemitter,我在学英语。我试图在单击第一个组件时将数据从一个组件发送到另一个组件 两个组件都是兄弟 这是我目前的代码: 第一部分: 这是服务: subscribe to stateChange中的函数从不运行。我错过了什么 编辑 这是mystateChangeEventEmitter的内容: _isAsync: true _isScalar: false destination: undefined dispatching: false hasCompleted: false hasErrored: fal

我在学英语。我试图在单击第一个组件时将数据从一个组件发送到另一个组件

两个组件都是兄弟

这是我目前的代码:

第一部分: 这是服务: subscribe to stateChange中的函数从不运行。我错过了什么

编辑 这是my
stateChange
EventEmitter的内容:

_isAsync: true
_isScalar: false
destination: undefined
dispatching: false
hasCompleted: false
hasErrored: false
isStopped: false
isUnsubscribed: false
observers: Array[0]
source:undefined

您有两个不同的
JsonChangeService
实例。这就是为什么在组件之间不接收消息的原因。您需要有一个实例服务,即在父组件上或在顶层,如下所示:

bootstrap(AppComponent, [JsonChangeService])

this.stateChange.next
的调用是否实际发出事件?我如何检查它?您不能简单地调用
this.stateChange.emit
?我已将
next
更改为
emit
它仍然不工作。我已经用更改更新了我的问题我还添加了stateChange content的内容我在导入AppComponent的main.ts上有它。但仍然不起作用Angular2文档中的建议是将其放在
AppComponent
上,除非绝对必要,否则不要放在引导程序中。请参阅“配置喷油器”是否从每个组件中删除提供程序?@Pablo从任何“子组件”的
提供程序
部分删除服务。
@Component({
  selector: 'jsonRendered',
  templateUrl: '../templates/jsonrendered.html',
  directives: [Card, CardTitle],
  providers: [JsonChangeService]
})

export class JsonRendered {
  private jsonObject: Object

  constructor (private jsonChangeService: JsonChangeService) {
    this.jsonChangeService = jsonChangeService
    this.jsonObject = jsonChangeService.jsonObject
    this.jsonChangeService.stateChange.subscribe(json => { this.jsonObject = json; console.log('Change made!') })
  }

  ngOnInit () {
    console.log(1)
  }

  ngOnChanges () {
    console.log(2)
  }

  renderJson () {
    console.log(3)
  }
}
_isAsync: true
_isScalar: false
destination: undefined
dispatching: false
hasCompleted: false
hasErrored: false
isStopped: false
isUnsubscribed: false
observers: Array[0]
source:undefined
bootstrap(AppComponent, [JsonChangeService])