简体   繁体   English

对于大多数相似但位置不同的组件,角度最佳实践是什么?

[英]What's the Angular best practice for components that are mostly similar but differ in places?

I'm working on an Angular 5 webapp, a somewhat standard social media portal. 我正在开发一个Angular 5 webapp,一个有点标准的社交媒体门户网站。 It contains pages like a news feed, photo album, events list etc... 它包含新闻提要,相册,活动列表等页面...

I need to add a feature called "Groups". 我需要添加一个名为“Groups”的功能。 Like any social network group, certain users can join the group. 与任何社交网络组一样,某些用户可以加入该组。 Unlike others, the idea of Groups is that the group members get their own news feed, photo album, events list etc. 与其他人不同,群组的想法是群组成员获得自己的新闻提要,相册,活动列表等。

This means that there is a lot of similarity between the non-group and group components, but they aren't exactly the same (a different button or div here or there, etc). 这意味着非组和组组件之间存在很多相似性,但它们并不完全相同(这里或那里有不同的按钮或div等)。

I'm new to Angular but not coding. 我是Angular的新手但没有编码。 This smells like a situation where I could easily give myself a hard time with a bad design. 这闻起来像是一种情况,我可以轻易地给自己一个糟糕的设计。 If I look at traditional OOP, I'd probably use either: 如果我看一下传统的OOP,我可能会使用:

  • Composition 组成
  • Inheritance 遗产

Over: 过度:

  • Passing an "is_groups" parameter to my component and filling it with if statements 将“is_groups”参数传递给我的组件并使用if语句填充它
  • Copying and pasting large parts of my hierarchy 复制并粘贴我的层次结构的大部分内容

What's the Angular best practice for this type of situation, where I have components that are similar to others, but differ based on a "state" (groups vs non-groups)? 对于这种情况,Angular最佳实践是什么,我的组件与其他组件类似,但根据“状态”(组与非组)不同?

NB At the moment, the components are probably going to be in different modules (groups being its own module), but this may change if that turns out to be a bad idea, for example if it makes the reuse I'm talking about above difficult. NB目前,组件可能会在不同的模块中(组是其自己的模块),但如果结果是一个坏主意,这可能会改变,例如,如果它使我重复使用上面的模块难。

I can't say it's the best practice since I'm also new to Angular2+ but this has been working for me since I had a similar case in one of my webapp. 我不能说这是最好的做法,因为我也是Angular2 +的新手,但这对我来说很有用,因为我在我的一个webapp中有类似的情况。

In your .ts file, I would add your desired states as boolean: 在你的.ts文件中,我会将你想要的状态添加为布尔值:

    state1 = false;
    state2 = false;
    state3 = false;

In your .html file, bind this state to a structural directive to show depending on the state. .html文件中,将此状态绑定到结构指令以根据状态显示。 I used ng-container with *ngIf mostly in all those cases because ng-container does not render if the condition is not met. 我主要在所有这些情况下使用带有* ngIf的 ng-container ,因为如果不满足条件, ng-container不会呈现。 With that in mind, you could choose to put everything you want between those ng-container without it ever rendering. 考虑到这一点,您可以选择将所需的一切放在那些ng-container之间,而不用渲染。 For this example, a button will display according to your states. 对于此示例,将根据您的状态显示一个按钮。

    <ng-container *ngIf="state1">
         <button (click)="function1()"> Bonuses </button>
        <!-- <div> </div> --> // Anything could go here !
    </ng-container>

    <ng-container *ngIf="state2">
         <button (click)="function2()"> Elite Bonuses </button>
         <!-- <div> </div> --> // Anything could go here !
    </ng-container>

    <ng-container *ngIf="state3">
         <button (click)="function3()"> Supreme Bonuses</button>
         <!-- <div> </div> --> // Anything could go here !
    </ng-container>

Of course you would need a logic that toggle the states, but that depends on how you intend to pass those states such as services or which ever you prefer. 当然,您需要一个切换状态的逻辑,但这取决于您打算如何传递服务或您喜欢的状态。

暂无
暂无

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

相关问题 向服务的可观察性订购组件时的最佳实践是什么? [Angular 2+] - What is the best practice when subscribing components to a service's observable? [Angular 2+] Angular 14 - 什么方法是最佳编码实践 angular 组件 - Angular 14 - what method would be best coding practice angular components 关于 Angular 中的 routerLink 和展示组件的最佳实践是什么? - What is the best practice regarding routerLink and presentational components in Angular? 在Angular 5中保存和读取图像的最佳实践是什么? - What's the best practice for saving and reading image in Angular 5? 使用Angular 6在两个地方使用非常相似的for循环是不好的做法吗? - Is it poor practice to use very similar for loops in two places with Angular 6? Angular中根据条件显示组件的最佳实践 - Best practice to display components based on conditions in Angular 在Angular组件中导入sass变量文件,最好的方法是什么? - importing sass variables file in Angular components, what's the best way? Angular - 在多个组件中包含 html 的最佳方法是什么? - Angular - What's the best way to include html in multiple components? 在 Angular 组件之间通过服务共享地图/对象/数组中的数据的最佳实践是什么 - What is the best practice to share data in map/object/array by service among Angular components angular 中订阅方法的最佳实践是什么? - What is the best practice for subscribe method in angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM