【问题标题】:child component not emitting output to parent component子组件不向父组件发出输出
【发布时间】:2021-05-14 18:24:45
【问题描述】:

我看到了与我的完全相似的问题,甚至我的应用程序在这个 stackblitz 中运行良好,但在我的本地项目中,它包含我的组件和模块,并且对于我的生活来说,我不知道为什么。

背景:

我决定添加 Login with Google,这可以从项目的两个部分进行。由于在域和其他方面应用了某种逻辑,因此我决定使用简单的 Open with Google Button 创建一个单独的子组件。

我成功地从 google 获取数据,但无法发送回父组件。摆脱 google 的概念,我什至不能像你在闪电战中看到的那样发出正常值。

子组件不向父组件发出输出的原因可能是什么?我在 Angular 7 中,这很重要。我的代码是:

google.component.ts

import { Component, OnInit, Output, EventEmitter } from "@angular/core";

@Component({
  selector: "app-google-login",
  template: `<button type="button" (click)="process()">
    Act
  </button>`
})
export class GoogleLoginComponent implements OnInit {
  @Output() userDetails = new EventEmitter<any>(); 

  constructor() {}

  process(): void {
    /*
    emit data here
    */
    this.userDetails.emit('Done')
  }

  ngOnInit() {}
}

App.component.html

<app-google-login
  (userDetails)="emittedGoogleUserInformation($event)"
></app-google-login>

app.component.ts

emittedGoogleUserInformation(info){
  console.log('google data from child component ', info);
}

【问题讨论】:

  • 控制台没有错误?尝试清除浏览器缓存并再次运行应用程序。
  • 它适用于我,在控制台中按预期输出。

标签: angular


【解决方案1】:

你可以试试这个:

@Output() userDetails: EventEmitter<any> = new EventEmitter<any>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-12
    • 2021-02-12
    • 2021-07-26
    • 2018-09-30
    • 2020-10-06
    • 2018-09-30
    • 2019-08-10
    • 2019-05-31
    相关资源
    最近更新 更多