简体   繁体   English

在Angular组件之间重用参数

[英]Reusing parameters among Angular components

When I'm sharing data among components should I call that data only once and provide it as @Input() or should I call that data again on every component's cycle? 当我在组件之间共享数据时,我应该只调用一次数据并将其提供为@Input()还是在每个组件的周期中再次调用该数据?

For example, I have the following components in one page: 例如,我在一页中包含以下组件:

<game-info [id]="params?.id"></game-info>
<game-leaderboard [uid]="auth" [id]="params?.id"></game-leaderboard>
<game-progress [uid]="auth" [id]="params?.id"></game-progress>

Where I get the id from the ActivatedRoute and the uid from my authentication service. 我在哪里从ActivatedRoute获得id ,从身份验证服务获得uid In some cases, I'm also passing a data input for multiple components in the same page. 在某些情况下,我还会在同一页面中传递多个组件的data输入。

One problem I face is that, sometimes, I'm passing data to many children components and it was harder to debug. 我面临的一个问题是,有时我会将数据传递给许多子组件,并且调试起来更加困难。 For example: 例如:

game.component.html game.component.html

<game-details [data]="data"></game-details>
<game-progress [data]="data"></game-progress>

Then, on details.component.html , I'd pass data as an input to another component - and so on. 然后,在details.component.html上 ,我将data作为输入传递给另一个组件-依此类推。 It became a really long chain: 它变成了一条很长的链:

<game-info [data]="data"></game-info>
<game-players [id]="(data | async)?.$key></game-players>

Is it a proper approach? 这是正确的方法吗? Or would it be better to get those parameters inside of every component? 还是将这些参数包含在每个组件中会更好?

Another issue, for example, sometimes I need to get the an async parameter (eg uid from an Observable) during onInit but it didn't receive it yet. 另一个问题,例如,有时我需要在onInit期间获取一个async参数(例如,来自Observable的uid ),但尚未收到它。 So, I was wondering if I should just call those parameters straight in the component instead of passing them as an input. 因此,我想知道是否应该直接在组件中直接调用这些参数,而不是将其作为输入传递。

PS. PS。 I'm not sure if this is off-topic. 我不确定这是否是题外话。 If so, please feel free to remove it. 如果是这样,请随时将其删除。

Nothing wrong with that approach. 这种方法没有错。 Actually, this is 1 of the recommended ways nowadays where your top-level 'smart' components would gather the data and then inject that data to your 'presentational' aka 'view' aka 'dumb' components. 实际上,这是当今推荐的方法之一,其中顶级“智能”组件将收集数据,然后将数据注入到“演示”或“查看”或“哑”组件中。 Note that data also flows the other way around: all Events are emitted upwards, where they are handled by the containing 'smart' component. 请注意,数据也以相反的方式流动:所有事件均向上发出,在此由包含的“智能”组件进行处理。 See here for a good (better) explanation. 请参阅此处以获得更好(更好)的解释。

I must say this approach has been working very well for me: code is clean (clear responsibilities, reusability and testability of presentational components...) although I must admit I share your concern that it might become a bit tedious when you have quite a lot of nested components. 我必须说这种方法对我来说一直很好:代码很干净(明确的职责,表示性组件的可重用性和可测试性...),尽管我必须承认我同意您的担心,当您拥有足够的代码时,它可能会变得有些乏味很多嵌套的组件。

A common approach would be using it as a Injectable service. 一种常见的方法是将其用作可Injectable服务。

For its benefits, as it says: 正如它所说的,它的好处是:

The component can create the dependency, typically using the new operator. 该组件通常可以使用new运算符来创建依赖关系。 The component can look up the dependency, by referring to a global variable. 该组件可以通过引用全局变量来查找依赖关系。 The component can have the dependency passed to it where it is needed. 组件可以在需要的地方将依赖项传递给它。

For angular 1, check https://docs.angularjs.org/guide/di 对于角度1,请检查https://docs.angularjs.org/guide/di

For angular 2, check the first answer in What's the best way to inject one service into another in angular 2 (Beta)? 对于角度2,请检查“ 以角度2(Beta)将一项服务注入到另一个服务的最佳方法什么”中的第一个答案

It is hard to answer the question since I am not sure exactly what you are trying to achieve but it might be worth looking into services. 很难回答这个问题,因为我不确定您到底想达到什么目的,但是可能值得研究服务。 Services will have one shared space for the components under the component it is declared under(example the app.component or even app.mudule ). 服务将在其声明下的组件下拥有一个共享空间(例如app.component甚至app.mudule )。 Then you can pass the new parameters to other components through the service. 然后,您可以通过服务将新参数传递给其他组件。 That would be the proper way of having a shared state and you can pass through many components by just injecting the service. 那将是拥有共享状态的正确方法,并且只需注入服务即可遍历许多组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM