简体   繁体   English

Observable.from([true])有什么作用?

[英]What does Observable.from([true]) do?

Looking through my code base, I've noticed that a previous developer, when creating a function to transform one model to another, commonly uses the following code: 通过我的代码库,我发现以前的开发人员在创建将一个模型转换为另一个模型的函数时,通常使用以下代码:

property: Observable.from([true]);

What does this accomplish exactly and why can't the value just be set to true ? 这究竟能完成什么工作,为什么不能将值仅设置为true

According to the documentation , the from operator: 根据文档from运算符:

Creates an Observable from an Array, an array-like object, a Promise, an iterable object, or an Observable-like object. 从数组,类似数组的对象,Promise,可迭代对象或类似Observable的对象创建一个Observable。

Why should we use the from operator if the array is only ever 1 value? 如果数组只有1个值,为什么还要使用from运算符?

I am guessing, these are conventions followed by some teams or individuals, where 我猜这是一些团队或个人遵循的惯例,

  1. members are initialised to determine type and initial/default value, here property is of type Observable<boolean> , which means this member data is calculated asynchronously (and is most likely to be used with an async pipe in the HTML incase of angular). 成员被初始化以确定类型和初始值/默认值,这里property的类型为Observable<boolean> ,这意味着该成员数据是异步计算的(在有角度的情况下很可能与HTML中的async管道一起使用)。
  2. for the sake of maintenance and understandability, one could use an array instead of a single value, just to indicate that this property emits multiple values instead of a single value. 为了维护和易于理解,可以使用数组而不是单个值,只是表明此属性发出多个值而不是单个值。 (Just informs developers to be aware of this during the design or implementation) (只是通知开发人员在设计或实施过程中要意识到这一点)

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

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