gpt4 book ai didi

javascript - 何时使用 @Input 以及何时不使用 angular2

转载 作者:行者123 更新时间:2023-12-02 07:21:39 25 4
gpt4 key购买 nike

我是 angular2 的新手。试图以更好的方式理解 @Input 的用法。我们可以使用 @Input 将值从父组件传递给子组件,我们也可以创建一个服务变量并将其注入(inject)父组件,然后可以在子组件中访问它们。哪种方式会更好?我通过路由解析获取这些值。当没有将它与用户输入绑定(bind)时,我对使用 @Input 持怀疑态度。

最佳答案

通常有两种类型的组件 - 展示组件和容器组件,有时也称为有状态组件和无状态组件。这是来自this article的专家解释差异:

Presentational components:

  • Are concerned with how things look.
  • Receive data and callbacks exclusively via props.
  • Rarely have their own state (when they do, it’s UI state rather than data).

Container components:

  • Are concerned with how things work.
  • Provide the data and behavior to presentational or other container components.
  • Are often stateful, as they tend to serve as data sources.

展示组件应该通过输入绑定(bind)以声明的方式接收尽可能多的数据。容器组件应尽可能使用 DI。

关于javascript - 何时使用 @Input 以及何时不使用 angular2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44299245/

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