gpt4 book ai didi

可观察类型的 Angular @Input

转载 作者:太空狗 更新时间:2023-10-29 18:18:30 25 4
gpt4 key购买 nike

将组件的@Input 传递给可观察对象是不好的做法吗?

例如: parent 的模板将有

<profile-edit [items$]="profileFacade.items$">

ProfileEditComponent 将有一个这样的变量:

@Input items$: Observable<ProfileItem[]>

并使用 |异步管道以取消转换模板中可观察值的值ProfileEditComponent 的

最佳答案

我认为这不是好的做法。 Angular 为您提供了一个 async 管道,这正是您在这里想要的。

当使用 async 管道时:

  • 生成的代码较少,因为您不需要先订阅可观察对象。

  • 组件不需要知道Observable类,这样比较笨

所以我认为:

<profile-edit [items]="profileFacade.items$ | async">


@Input items: ProfileItem[]

比这更干净更好:

<profile-edit [items]="profileFacade.items$">`


@Input items: Observable<ProfileItem[]>`

只是我的猜测,我不是专家。

关于可观察类型的 Angular @Input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51440182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com