首页 \ 问答 \ Angular订阅/取消订阅最佳实践(Angular subscribe / unsubscribe best practices)

Angular订阅/取消订阅最佳实践(Angular subscribe / unsubscribe best practices)

我目前想知道角度订阅和取消订阅。 关于这个问题有很多东西,所以我在所有这些方面都有点迷失。

我什么时候应该取消订阅? 如果我没有退订他们,会怎么样? 我从来没有遇到过reliquat订阅的任何错误。

有没有办法自动取消订阅组件/应用程序中的所有内容,这样我就不必通过订阅来声明1个属性了? 这可能非常烦人:

@Component({
  selector: 'subscriptionTest',
  template: `...`,
})
export class SubTestComponent {
  first;
  second;
  third;

  constructor(private remote: RemoteService){}

  ngOnInit() {
    this.first = remote.getSomeData().subscribe(data => // do something);
    this.second = Observable.interval(500).subscribe(event => // do something);
    this.third = remote.getSomeOtherData().subscribe(data => // do something);

  }

  ngOnDestroy() {
    this.first.unsubscribe();
    this.second.unsubscribe();
    this.third.unsubscribe();
  }

}

I'm currently wondering about angular subscription and unsubscription. There is a lot of stuff on the subject, so I'm a bit lost in all of this.

When should I unsubscribe from subscription ? What happens if I don't, ever, unsubscribe from them ? I never encounter any errors from reliquat subscription.

Is there a way to auto unsubscribe from everything in a component/app, so that I don't have to declare 1 property by subscription ? This can be very annoying :

@Component({
  selector: 'subscriptionTest',
  template: `...`,
})
export class SubTestComponent {
  first;
  second;
  third;

  constructor(private remote: RemoteService){}

  ngOnInit() {
    this.first = remote.getSomeData().subscribe(data => // do something);
    this.second = Observable.interval(500).subscribe(event => // do something);
    this.third = remote.getSomeOtherData().subscribe(data => // do something);

  }

  ngOnDestroy() {
    this.first.unsubscribe();
    this.second.unsubscribe();
    this.third.unsubscribe();
  }

}

原文:https://stackoverflow.com/questions/47152752
更新时间:2022-06-07 10:06

最满意答案

当执行重复加法时,发生了9次舍入:第一个结果a+a总是可以精确表示,但之后,由于加数的基数2指数不相等,所以进一步加法是不精确的。

执行乘法时,只有一个舍入,它恰好给你想要的结果。


When performing the repeated addition, there are 9 roundings taking place: the first result a+a is always exactly representable, but after that, further additions are inexact since the base-2 exponents of the addends are not equal.

When performing the multiplication, there's only a single rounding, and it happens to give you the result you wanted.

相关问答

更多
  • 对于“更精确”: Python Cookbook中的这个配方具有保持完整精度(通过跟踪小计)的求和算法。 代码是用Python编写的,但即使你不了解Python,它也足够清楚地适应任何其他语言。 所有细节都在本文中给出。 For "more precise": this recipe in the Python Cookbook has summation algorithms which keep the full precision (by keeping track of the subtotals) ...
  • 鉴于您的最后两个示例,使用4543621235648200192等,似乎您希望接受的数字是.0001的整数倍,并拒绝那些不是, 并且您希望这样做而不使用.0001但使用包含浮动的变量 -点值最接近.0001。 一般来说,这是不可能的。 当你传递其他东西时,算法无法“知道”.0001是有意的。 如果您更多地限制问题,则可能有解决方案。 例如,有可能(可能不容易)回答这个问题:浮点值X是否最接近.0001的整数倍的浮点值? (换句话说,是否存在一个整数k,使得.0001乘以k并舍入到最近的浮点值会产生正好的X? ...
  • 因为1.0 + ((10.0 - 1.0) / 10.0) * 10.0只用不精确的值进行1次计算,因此有1次舍入误差,所以比10次加入浮点数表示0.9f更准确。 我认为这是本例中要教授的主要内容。 关键问题是0.1不能精确表示浮点数。 所以0.9中有错误,它在函数循环中加起来。 由于巧妙的输出格式化例程,可能会显示“确切”数字。 当我第一次使用计算机时,他们喜欢用荒谬的科学固定数字格式输出这些数字,这种格式不是人类友好的。 我想要明白发生了什么我会找到Koenig的Dobbs博士关于这个主题的博客文章,这 ...
  • 正如其他几位用户所评论的那样,问题不在于您的计算,而在于输出字符串的格式。 %d格式化序列告诉Python在将其转换为字符串之前将相关值转换为整数。 使用不同的代码可以使其按照您期望的方式工作。 最明显的代码是%f ,这表明你想要一个浮点值。 如果您希望浮点数四舍五入到特定的小数位数(而不是默认值可能很长),则可以添加一些额外的字符。 例如使用%.4f会舍入四位小数,这可能是一个不错的选择,因为这是您在示例输入中使用的有效数字的最大数目。 另一个选择是使用%s ,这意味着你想使用内建的str函数直接将参数转 ...
  • 简短回答: 2的平方根需要无限数量的数字 - 即无限精度。 double s有52位精度。 这是很多,但它远远不是无限的。 如果你试图用两位数(0.33)表示1/3,你不会对舍入错误感到惊讶,对吗? 你将它乘以3并且不会惊讶地得到0.99而不是1.0的答案。 这完全是一回事。 进一步挖掘...... 更直观的是,可以用基数10中的无限位数表示的数字可能无法用基数2中的有限位数表示(这是double s和float使用的数字) 。 例如,1/10在基数为10时为0.1,但在基数为2时为0.0001100110 ...
  • bc期待标准输入。 由于第一个命令没有标准输出,因此在这种情况下bc没有输入。 尝试这个: sum=`echo $sum+${NUMBERS[$i]} | bc -l` bc is expecting standard input. Since the first command has no standard output, bc has no input in this case. Try this: sum=`echo $sum+${NUMBERS[$i]} | bc -l`
  • 如果要确保浮点乘法是精确的,则需要确保乘以的任何两个数字永远不会超过浮点类型中有空格的数字的一半。 究竟。 这种技术可以在Veltkamp / Dekker乘法中找到。 虽然可以像在其他答案中一样访问表示的位,但您也可以仅使用浮点运算。 这篇博文中有一个例子。 您感兴趣的部分是: Input: f; coef is 1 + 2^N p = f * coef; q = f - p; h = p + q; // h contains the 53-N highest bits of f l = f - ...
  • 当执行重复加法时,发生了9次舍入:第一个结果a+a总是可以精确表示,但之后,由于加数的基数2指数不相等,所以进一步加法是不精确的。 执行乘法时,只有一个舍入,它恰好给你想要的结果。 When performing the repeated addition, there are 9 roundings taking place: the first result a+a is always exactly representable, but after that, further additions ar ...
  • 从Python文档: 浮点数通常在C中使用double实现; sys.float_info中提供了有关运行程序的机器的浮点数的精度和内部表示的信息。 sys.float_info是一个结构序列,包含有关float类型的信息。 它包含有关精度和内部表示的低级信息。 import sys print(sys.float_info.mant_dig) mant_dig属性指向浮点精度:浮点数有效数中的基数 - 数字 几乎所有平台都将Python浮点数映射到IEEE 754双精度。 From Python do ...
  • 我的目标是在不同平台(C#/ AS3和32/64位)之间实现更高的一致性,同时我接受100%的一致性是不可能的。 (由于AS3不能进行实数整数运算,因为所有内部运算都是通过浮点数执行的) 到目前为止我收集的内容(感谢Eric Postpischil和Jeffrey Sax): Math.round(1024 * float) / 1024; 除此之外 ,“ Math.round(1024 * float) ”操作可能不会在所有平台上产生相同的结果,如果“ 错误累积到量子的一半以上 ”,即使“ 在单个操作中 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。