# 组件的实时更新

**URL:** https://meta.discourse.org/t/live-updates-of-components/318126
**Category:** Development
**Created:** [2024年七月24日 14:15 UTC](https://meta.discourse.org/t/live-updates-of-components/318126 "2024-07-24T14:15:26Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2024年七月24日 16:55 UTC](https://meta.discourse.org/t/live-updates-of-components/318126/2 "2024-07-24T16:55:11Z")

</div>

所以，我做到了。我认为这些片段可能对其他人有所帮助，所以这是我在组件中遗漏的内容：

这远非通用的有用文档，但也许它能在某人需要时帮助到他们（或者下次我需要时能帮到我自己！）。

```plaintext
import { tracked } from '@glimmer/tracking';
...
  @tracked myRating

 constructor() {
    super(...arguments);
    this.userRatings = this.args.topic.user_ratings; // 初始化跟踪属性
    if (!this.args.topic.user_ratings) {
      return [];
    }
    const ratingId = this.args.id;
    const rating = this.args.topic.user_ratings.find((rating) => Number(rating.rating_id) === Number(ratingId));
    this.myRating = rating?.rating_value;
    }

....
 // 然后在更改值的操作中：
        this.myRating = Number(newStatus);

```

---

_[View the full topic](https://meta.discourse.org/t/live-updates-of-components/318126)._
