angular2 风格指南 - 带有美元符号的属性? [英] angular2 style guide - property with dollar sign?

查看:26
本文介绍了angular2 风格指南 - 带有美元符号的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

父母和孩子通过服务示例进行交流 来自 Angular.io 的官方指南在 Observable 流名称中使用美元符号.

Parent and children communicate via a service example from the official guide on Angular.io makes use of dollar signs in Observable stream names.

注意以下示例中的missionAnnounced$missionConfirmed$:

Notice missionAnnounced$ and missionConfirmed$ in the following example:

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);
  }
}

谁能解释一下:

  • 为什么使用 $?这个符号背后的原因是什么?我是否总是需要将其用于公共财产?
  • 使用公共属性,但不使用方法(例如,missionAnnouncements()、missionConfirmations())——同样,这是 Angular2 应用程序的约定吗?
  • 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 (e.g. missionAnnouncements(), missionConfirmations()) - again, is this a convention for Angular2 apps?

推荐答案

$ 后缀(流行于 Cycle.js) 用于表示该变量是一个 Observable.它也可以进入官方风格指南,但还没有

$ suffix (popularized by Cycle.js) is used to indicate that the variable is an 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?

更新:在此处阅读有关 Angular 网站上尾随$"符号的更多信息:https://angular.io/guide/rx-library#naming-conventions-for-observables

Update: Read more about the trailing "$" sign on Angular website here: https://angular.io/guide/rx-library#naming-conventions-for-observables

这篇关于angular2 风格指南 - 带有美元符号的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆