首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用@Output在父组件和子组件之间通信的角6

使用@Output在父组件和子组件之间通信的角6
EN

Stack Overflow用户
提问于 2019-03-08 17:00:43
回答 3查看 121关注 0票数 0

我正在做一个6角的项目。

我需要在父组件和子组件之间进行通信(从父组件到子组件),但是实际上,通过使用@Output,我无法做到这一点。

请帮助我了解以下代码。

儿童component:survey.component.html

<div style="text-align:center"> <app-root (numberGenerated)='selectValue($event)'></app-root> survey.component.ts

代码语言:javascript
复制
import { Component, OnInit, SkipSelf , Input, Output , EventEmitter} from '@angular/core';
import { AppComponent } from '../parent/app.component'

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

selectValue( newValue : any ) {
 console.log(newValue);
}
constructor(){}

ngOnInit() {
}

}

父组件: app.component.ts

代码语言:javascript
复制
import { Component, Input , Output , EventEmitter } from    '@angular/core';
import { Router } from '@angular/router'; 

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'BegumSurvey';

@Output() private numberGenerated = new EventEmitter<number>();

 public generateNumber() {
   const randomNumber = Math.random();
   this.numberGenerated.emit(randomNumber);
 }

 constructor(){
 }
 ngOnInit() {

 }
 }

app.component.html

代码语言:javascript
复制
<button class="btn" (click)="generateNumber()">Fire event!</button>

你能帮我理解一下为什么“火灾事件”吗?不是印刷的?

非常感谢。

任何帮助都是非常感谢的。

贝古姆

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-03-08 17:51:58

如果要将数据从父组件传递给子组件,则需要单独使用@Input装饰符和属性绑定。下面是基于您的说明的示例代码。

代码语言:javascript
复制
survey-child.component.ts

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

@Component({
  selector: 'app-survey-child',
  templateUrl: './survey-child.component.html',
  styleUrls: ['./survey-child.component.css']
})
export class SurveyChildComponent implements OnInit {
  @Input() surveyChildValue: string;
  public testValue: string;

  constructor() { }

  ngOnInit() {
    this.testValue = this.surveyChildValue;
    this.selectValue();
  }

  selectValue() {
    console.log(this.surveyChildValue);
    
  }

}

survey-parent.component.ts

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

@Component({
  selector: 'app-survey-parent',
  templateUrl: './survey-parent.component.html',
  styleUrls: ['./survey-parent.component.css']
})
export class SurveyParentComponent implements OnInit {
  parentValue: string = 'Angular 6 Communicating between Parent&Child components using @Input';
  constructor() { }

  ngOnInit() {
  }

}
代码语言:javascript
复制
survey-child.component.html

<!--This will print the Value you assignned in Parnet in UI as we use interpretation -->
<p>
  {{testValue}}
</p>

survey-parent.component.html

<app-survey-child [surveyChildValue]="parentValue"></app-survey-child>




app.component.html
<router-outlet>
  <app-survey-parent></app-survey-parent>

</router-outlet>

在这里输入图像描述

票数 1
EN

Stack Overflow用户

发布于 2019-03-08 17:09:30

我需要在父组件和子组件之间进行通信(从父组件到子组件),但是实际上,通过使用@Output,我无法做到这一点。

@Output是用于儿童与父母之间的交互。您需要的是使用@Input (亲子交互):

父组件(app.ts):

代码语言:javascript
复制
this.numberGenerated = Math.random();

父组件(app.html):

代码语言:javascript
复制
<app-survey [newValue]="numberGenerated"></app-survey>

子组件(survey.component.ts):

代码语言:javascript
复制
import { Component, OnInit, SkipSelf , Input, Output, Input, EventEmitter} from 
'@angular/core';
import { AppComponent } from '../parent/app.component'

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

  @Input() newValue;
  ...

@Output是孩子的EventEmitter属性。更多信息在这里:角分量相互作用

票数 0
EN

Stack Overflow用户

发布于 2019-03-08 19:23:01

对于@Output和EventEmitter,情况正好相反。您可以将数据从子组件传回父组件。同样,在子表中,我们声明了一个变量,但这次使用@Output装饰符和一个新的EventEmitter

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55067750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档