$在Angular 7中是什么意思

what does $ mean in Angular 7

我对变量声明有些困惑。

what does $ mean in heroes$

Angular 4

export class HeroSearchComponent implements OnInit {
heroes: Observable<Hero[]>;
private searchTerms = new Subject<string>();

constructor(
 private heroSearchService: HeroSearchService,
 private router: Router) {}

Angular 7+

 export class HeroSearchComponent implements OnInit {
 heroes$: Observable<Hero[]>;
 private searchTerms = new Subject<string>();
 constructor(private heroService: HeroService) {}

     // Push a search term into the observable stream.
          search(term: string): void {
          console.log(term);
            this.searchTerms.next(term);
        }

这是 Observables 遵循的约定。以下是 Angular Docs 对此的评价:

Because Angular applications are mostly written in TypeScript, you will typically know when a variable is an observable. Although the Angular framework does not enforce a naming convention for observables, you will often see observables named with a trailing “$” sign.

This can be useful when scanning through code and looking for observable values. Also, if you want a property to store the most recent value from an observable, it can be convenient to simply use the same name with or without the “$”.

不遵循它本身并没有什么坏处。不过既然是推荐,照着做就好了。

更新

惯例根据开发人员过去的经验随着时间的推移而演变。这个特定的约定是 committed on the 10th of Jan, 2018

是的,此约定是在 Angular 5 之后添加到文档中的,并且您在 Angular 4 中工作时很可能没有使用它。

此外,在 Angular5 中升级到 Rxjs 5.5 后,Rxjs 的语法也发生了显着变化(不确定确切的版本)。所以你可能想检查 Rxjs 的语法是如何随时间变化的。有一个漂亮的工具可以帮助您。查看 RxJS Explorer 2.0: 学。比较。更新。