我什么时候应该在angular 2中使用zone.run [英] When should i use zone.run in angular 2

查看:92
本文介绍了我什么时候应该在angular 2中使用zone.run的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试阅读了很多有关角度2中区域的信息.我知道zone.js的角度用途用于变化检测.我看到一些代码使用zone.run并在其中放置了一些操作.
它实际上是做什么的?我了解的一件事是,它将这些命令作为单独的块执行.我仍然不知道zone.run

I tried to read a lot about zones in angular 2. I got to know that angular uses of zone.js for change detection. I see some code used zone.run and place few actions inside of it.
What does it do actually? One thing I understood is that it executes these commands as separate block. Still I could not figure out the actual use of zone.run

推荐答案

要了解有关NgZone的更多信息,请阅读:

To learn more about NgZone read:

它实际上有什么作用?

What does it do actually?

Zone.run允许您更改当前区域.这是基本示例:

Zone.run allows you to change the current zone. Here is the basic example:

console.log(Zone.current.name);  // <root>
setTimeout(() => console.log(Zone.current.name));  <root>

// we're in the <root> zone now, but when we call `run` on the instance of zone `A` we change the zone
Zone.current.fork({name: 'A'}).run(() => {
    console.log(Zone.current.name); // A
    setTimeout(() => console.log(Zone.current.name)); // A
});

// again we're back in the <root> zone now, but when we call `run` on the instance of zone `B` we change the zone   
Zone.current.fork({name: 'B'}).run(() => {
    console.log(Zone.current.name);  // B
    setTimeout(() => console.log(Zone.current.name)); // B
});

Zone.current.fork创建一个新区域.它返回对该新区域的引用,并且通过使用run您可以在该区域内执行异步操作.您可以在上方看到setTimeout异步任务在整个时间段内都保持正确的区域.

Zone.current.fork creates a new zone. It returns the reference to this new zone and by using run you can execute an asynchronous action inside this zone. You can see above that the setTimeout async task persists correct zone across the time.

Angular创建自己的区域:

Angular creates its own zone:

function forkInnerZoneWithAngularBehavior(zone: NgZonePrivate) {
  zone._inner = zone._inner.fork({
    name: 'angular',
    properties: <any>{'isAngularZone': true},
    ...

这个新区域就是将NgZone注入类构造函数时得到的:

And this new zone is what you get when you inject NgZone into class constructors:

constructor (zone: NgZone) {}

当当前执行区域不是Angular区域(NgZone)-Zone.current.name !== 'angular'时,您需要使用zone.run().如果是这种情况,则您需要从该区域安排的所有异步任务都不会被Angular接收,也不会安排更改检测.因此,您可以切换到Angular区域执行一些异步任务:

You need to use zone.run() when the current execution zone is not Angular zone (NgZone) - Zone.current.name !== 'angular'. If it's the case you need all async tasks scheduled from this zone will not be picked up by Angular and it won't schedule change detection. So you can switch to Angular zone for some async tasks:

Zone.current.fork({name: 'A'}).run(() => {
    // this timeout won't be picked up by Angular
    setTimeout(() => console.log(Zone.current.name)); // A

     // this timeout will be picked up by Angular
     NgZone.run(() => setTimeout(() => console.log(Zone.current.name)));
});

有时候,您也可能做相反的事情,并在Angular区域之外运行一些异步任务.为此使用runOutsideAngular方法:

Sometimes you may also do the opposite and run some async tasks outside of Angular zone. Use runOutsideAngular method for that:

NgZone.runOutsideAngular(()=>{ ... })

这篇关于我什么时候应该在angular 2中使用zone.run的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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