1

I would like to use zone.js in my node app.

Although I have found a post in which is described how to do this, I still get zone is not defined error

For example. I also found an examples likt this

let zone = require('zone');

zone.run(function () {
    zone.inTheZone = true;

   setTimeout(function () {
       console.log('in the zone: ' + !!zone.inTheZone);
   }, 0);
});

Which gives the same error, but I inspected the zone object, I noticed that it returns an object like this

{
    enable: function() { .. }
}

So I guess that the zone API changed. Any help would be appreciated

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

1 Answers1

2

There are two things:

  1. require zone.js instead of zone
  2. use Zone.current to get reference to the current zone

This works:

const zone = require('zone.js');

Zone.current.run(function () {
    zone.inTheZone = true;

    setTimeout(function () {
        console.log('in the zone: ' + !!zone.inTheZone);
    }, 0);
});
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • 1
    way too broad question) you might find [this doc](https://docs.google.com/document/d/1F5Ug0jcrm031vhSMJEOgp1l-Is-Vf0UCNDY-LsQtAIY/edit#heading=h.e5oec3xkpw1r) very useful – Max Koretskyi Aug 19 '17 at 18:56
  • Great doc, thnx! but with `zone` I didn't mean `zone.js` but your const in your example. I guess its a reference to that zone – Jeanluca Scaljeri Aug 20 '17 at 08:22
  • 1
    @JeanlucaScaljeri, yeah, you actually don't need to save `require('zone.js')` to any variable since you always have reference to the current zone with `Zone.current` or `with Zone.current.fork` returns a reference to the zone. – Max Koretskyi Aug 20 '17 at 08:46