为什么要在主题/服务上使用@input @output?

问题描述 投票:0回答:1

通常在将数据从子级传递给父级或从父级传递给子级时,我们使用@input和@output @input 和 @output 相对于主题或服务有什么好处,除了它是 Angular 本身提供的最有机的方式之外。 它与变化检测有什么关系吗?

提前致谢

angular angular-components angular-changedetection data-sharing angular-lifecycle-hooks
1个回答
0
投票

一个非常简单的用例是:

@Component({
    selector: 'app-hello'
})
export class HelloComponent {
    @Input() username;
}
<!-- hello.component.html -->
<h1>Hello {{ username }}</h1>
<!-- app.component.html -->
<app-hello username="Peter"></app-hello>
<app-hello username="Pan"></app-hello>

我们拥有输入和输出的主要原因是它们允许组件之间进行数据通信,而无需将它们耦合在一起。

© www.soinside.com 2019 - 2024. All rights reserved.