简体   繁体   English

angular2 风格指南 - 带有美元符号的属性?

[英]angular2 style guide - property with dollar sign?

Parent and children communicate via a service example from the official guide on Angular.io makes use of dollar signs in Observable stream names. 父母和孩子通过Angular.io官方指南中的服务示例进行通信,在 Observable 流名称中使用美元符号。

Notice missionAnnounced$ and missionConfirmed$ in the following example:请注意以下示例中的missionAnnounced$missionConfirmed$

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable()
export class MissionService {

  // Observable string sources
  private missionAnnouncedSource = new Subject<string>();
  private missionConfirmedSource = new Subject<string>();

  // Observable string streams
  missionAnnounced$ = this.missionAnnouncedSource.asObservable();
  missionConfirmed$ = this.missionConfirmedSource.asObservable();

  // Service message commands
  announceMission(mission: string) {
    this.missionAnnouncedSource.next(mission);
  }

  confirmMission(astronaut: string) {
    this.missionConfirmedSource.next(astronaut);
  }
}

Can anyone explain:谁能解释一下:

  • why $ is used?为什么使用$ What's the reason behind this notation?这个符号背后的原因是什么? Do I always need to use this for public properties?我是否总是需要将它用于公共财产?
  • public properties are used but not methods (eg missionAnnouncements(), missionConfirmations()) - again, is this a convention for Angular2 apps?使用公共属性但不使用方法(例如,missionAnnouncements()、missionConfirmations()) - 同样,这是 Angular2 应用程序的约定吗?

$ suffix (popularized by Cycle.js ) is used to indicate that the variable is an Observable . $ 后缀(由Cycle.js流行)用于表示变量是一个Observable It could make it to the official style guide too but it's not there yet它也可以进入官方风格指南,但还没有

Read more here : What does the suffixed dollar sign $ mean?在此处阅读更多信息: 后缀美元符号$是什么意思?

Update: Read more about the trailing “$” sign on Angular website here: https://angular.io/guide/rx-library#naming-conventions-for-observables更新:在此处阅读有关 Angular 网站上尾随“$”符号的更多信息: https : //angular.io/guide/rx-library#naming-conventions-for-observables

The $ naming paradigm originated with Andre Saltz and suggests pluralizing all variable names that contain observables or streams. $ 命名范式起源于 Andre Saltz,并建议将所有包含 observable 或流的变量名称复数化。

getAll(): Observable<Zone[]>{
    let zone$ = this.http
      .get(`${this.baseUrl}/zones`, {headers: this.getHeaders()})
      .map(mapZone);
      return zone$;
  }

Another approach is to pluralize variable names that contain observables or streams with a unicode character that matches the last letter of the word.另一种方法是使用与单词的最后一个字母匹配的 unicode 字符将包含 observable 或流的变量名称复数化。 This addresses the issue with words that aren't pluralized with an "s".这解决了没有用“s”复数的单词的问题。

mouse$ vs mic€

Neither of these naming conventions are in the official Angular style guide.这些命名约定都没有出现在官方的 Angular 风格指南中。 Usage of one or the other (or none) is entirely dependent on personal preference.使用其中之一(或不使用)完全取决于个人喜好。

Update : https://angular.io/guide/rx-library#naming-conventions-for-observables更新 https : //angular.io/guide/rx-library#naming-conventions-for-observables

Because Angular applications are mostly written in TypeScript, you will typically know when a variable is an observable.因为 Angular 应用程序大多是用 TypeScript 编写的,所以您通常会知道变量何时是可观察的。 Although the Angular framework does not enforce a naming convention for observables, you will often see observables named with a trailing “$” sign.尽管 Angular 框架没有强制执行 observable 的命名约定,但您经常会看到以尾随“$”符号命名的 observable。

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 “$”.此外,如果您希望属性存储来自 observable 的最新值,只需使用带有或不带有“$”的相同名称会很方便。


Original :原文

I saw variables end with $ when reading the official hero tutorial:我在阅读官方英雄教程时看到变量以$结尾:

<div id="search-component">
  <h4>Hero Search</h4>

  <input #searchBox id="search-box" (keyup)="search(searchBox.value)" />

  <ul class="search-result">
    <li *ngFor="let hero of heroes$ | async" >
      <a routerLink="/detail/{{hero.id}}">
        {{hero.name}}
      </a>
    </li>
  </ul>
</div>

Look closely and you'll see that the *ngFor iterates over a list called heroes$ , not heroes .仔细观察,您会发现 *ngFor 遍历名为 hero heroes$的列表,而不是heros 。

<li *ngFor="let hero of heroes$ | async" >

The $ is a convention that indicates heroes$ is an Observable, not an array. $ 是一个约定,表示 hero$ 是一个 Observable,而不是一个数组。

Most cases are that we do not subscribe to those Observable variables in component.大多数情况是我们没有订阅组件中的那些 Observable 变量。 We usually use AsyncPipe to subscribe to the Observable variables automatically我们通常使用 AsyncPipe 自动订阅 Observable 变量

I haven't found it in Style Guide since Angular5.1 has released yesterday(December 6th, 2017).自从 Angular5.1 昨天(2017 年 12 月 6 日)发布以来,我还没有在 Style Guide 中找到它。

我没有在样式指南中看到这个$ ,但我看到它经常用于引用可以订阅的 observable 的公共属性。

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

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