【问题标题】:Is this way of making a Dropdown using Angular bad practice? [closed]这种使用 Angular 进行下拉的方式是不好的做法吗? [关闭]
【发布时间】:2021-09-24 11:48:25
【问题描述】:

我是 Angular 的新手,我最近尝试使用它创建我的第一个下拉菜单,并且效果非常好。但由于它引起了很多比较,我想知道这种做法是否是不好的做法。

我有一个看起来像这样的模板:

<div id="wrapper">
    <div class="dropdown-wrapper" *ngFor="let location of locations;">
        <div class="dropdown-title" (click)="showDropItems(location)">{{ location }}</div>
        
        <div *ngFor="let bot of bots;">
            <div *ngIf="bot.location === location && showItems(location)" class="dropdown-item">
                {{bot.id}}
            </div>
        </div>
    </div>

在这里,我首先只渲染唯一的位置,这些位置存储在类组件中定义的位置集中。 Onclick 然后我切换位置显示地图的值,因此在下面的 div 中我可以将这些值与当前正在循环的机器人的位置值进行比较。这样,下拉项目(机器人)显示在正确的位置下,并且只有在匹配的位置被点击时才可见。机器人的数据是从 JSON 文件中获取的。

类文件如下:

constructor(private http: HttpClient, private botService: BotService) { }

  public bots: Array<Bot> = [];
  public locations = new Set();
  public locationsShow = new Map<string, boolean>();

  ngOnInit(): void {
    this.getAllBots();
  }

  showItems(location: any)
  {
    return this.locationsShow.get(location);
  }

  showDropItems(location: any)
  {
    this.locationsShow.set(location, !this.locationsShow.get(location))
  }

  getAllBots() {
    this.botService.getAllBots()
      .subscribe(response => {
        this.bots = response;
        console.log(response);
        this.bots.forEach((bot, i) =>{
          this.locations.add(bot.location);
          this.locationsShow.set(bot.location, false);
        })
      }

      )
  }

由于函数被经常调用,因此在遍历 for 循环时会引起很多比较,我想知道这是不好的做法还是没问题。提前致谢。

【问题讨论】:

  • 不处理您的订阅绝对是个坏习惯!确保将订阅调用的结果 (this.subscription = this.obs$.subscribe()..) 保存在变量中,并在 OnDestroy -> this.subscription.unsubscribe() 中取消订阅。这样,每当组件被销毁和重新创建时,您就不会产生内存泄漏
  • 有一个很好的资源可以解释您的疑问,但我认为您已经意识到您的问题。 medium.com/showpad-engineering/… 对于这么便宜的支票,我不会太担心。 .如果您关心性能,您可能想尝试 OnPush 策略
  • @michal.materowski 你说得对,感谢您提供的资源!

标签: javascript node.js angular ecmascript-6


【解决方案1】:

这个

*ngIf="bot.location === location && showItems(location)"

导致showItems(location) 函数在每个更改检测周期中每个项目调用一次(这可能在一秒钟内发生多次,除非您将组件设置为 OnPush)。 当您进行这种绑定时,对象字典就是您的朋友:

TS:

constructor(private http: HttpClient, private botService: BotService) { }

  public bots: Array<Bot> = [];
  public locations = new Set();
  public locationsShow: {[index: string]: boolean} = {};

  ngOnInit(): void {
    this.getAllBots();
  }

  showDropItems(location: any)
  {
    this.locationsShow[location] = !this.locationsShow[location];
  }

  getAllBots() {
    this.botService.getAllBots()
      .subscribe(response => {
        this.bots = response;
        console.log(response);
        this.bots.forEach((bot, i) =>{
          this.locations.add(bot.location);
          this.locationsShow[bot.location] = false;
        })
      }

      )
  }

HTML

<div id="wrapper">
    <div class="dropdown-wrapper" *ngFor="let location of locations;">
        <div class="dropdown-title" (click)="showDropItems(location)">{{ location }}</div>
        
        <div *ngFor="let bot of bots;">
            <div *ngIf="bot.location === location && locationsShow[location]" class="dropdown-item">
                {{bot.id}}
            </div>
        </div>
    </div>

此外,您应该将 showDropItems 重命名为 toggleDropItems 并通过在 onDestroy 中取消订阅来处理您的 .subscribe

【讨论】:

  • 非常感谢您的回答!
  • 如果它确实可以帮助您解决问题,请随意点赞并将其标记为解决 :) 我从 Angular 版本 2 开始就一直在使用它,并且自从我开始绑定到各种字典 -动态性能关键场景,我实际上可以享受生活
  • 这是在字典中缓存此类计算的非常方便的模式,肯定会使用 +1!
猜你喜欢
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
  • 2019-02-10
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多