【发布时间】:2018-06-13 21:25:47
【问题描述】:
我有两个组件,我想在点击时从父组件打印子组件中的文本。
父组件:
import {Component, OnInit, ViewChild} from '@angular/core';
@Component({
selector: 'parent',
templateUrl: 'parent.html'
})
export class ParentComponent implements OnInit {
@ViewChild(ChildComponent) child;
constructor() {
}
ngOnInit() {
}
click(){
console.log(this.child.text);
}
}
子组件:
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'child',
templateUrl: 'child.html'
})
export class ChildComponent implements OnInit {
constructor() {
}
ngOnInit() {
const text = 'TEXT HERE';
}
//Some code...
}
我是 Angular 的新手。只是想知道如何使它工作,我希望一些常量在一个点上并由其他人共享。常量不必仅在子组件中。只需要一个关于如何使其与良好的编码策略一起工作的好建议
这对我不起作用。
谢谢
【问题讨论】:
-
发布你的父组件模板,你需要使用@input
-
你应该使用
@Output参考这个链接stackoverflow.com/questions/42107167/…
标签: angular typescript