angular - 在另一个组件中使用 @output 值
问题描述
我有一个发出值的组件@Output
和另一个我想使用该值的组件。
我如何myValue
从OutputComponent
到InputComponent
?
输出组件
export class OutputComponent implements OnInit {
@Output() myValue: EventEmitter<any> = new EventEmitter();
...
}
输入组件
export class InputComponent implements OnInit {
// Use myValue here
}
解决方案
如果“输入组件”是父组件:
在 child.component.ts 中:
@Output() someEvent = new EventEmitter
someFunction(): void {
this.someEvent.emit('Some data...')
}
在父模板中:
<app-child (someEvent)="handleSomeEvent($event)"></app-child>
在 parent.component.ts 中:
handleSomeEvent(event: any): void {
// Do something (with the event data) or call any functions in the component
}
如果它是一个不相关的组件,您需要使用您可以从应用程序中的任何位置订阅的主题将服务绑定到应用程序的根目录。
我在这里记录了组件之间所有类型的数据共享:
https://github.com/H3AR7B3A7/EarlyAngularProjects/tree/master/dataSharing
推荐阅读
- ruby-on-rails - Rails 6、Webpack、Bootstrap 5 - Bootstrap 无法在生产环境中编译
- python - 如何在使用带有请求库的 POST 方法时修复 406 状态代码
- python - python返回xmlblock并删除编码标签
- java - 检查字符串是否存在于 HashSet Java 中的时间复杂度
- r - 创建每个采样站点的唯一访问列
- android - 使用 Gradle 7.1.0-alpha03 和 Composer 的新 Android 项目无法构建
- excel - Excel - 格式化一个单元格,使其显示另一个单元格的算术表达式
- python - 在 Windows Python 中处理权限被拒绝错误
- xcode13 - XCode 13 beta 2,iOS 15 上的应用安装失败
- python-3.x - 使用 Python 多处理并行加载大型数据集