From 8df70b64fba08d8be8ff9fff58d0e39c779c7a33 Mon Sep 17 00:00:00 2001 From: Ankit Date: Mon, 2 Jul 2018 16:33:48 +0530 Subject: [PATCH] feat(Core): fix #910, add a flag to allow user to ignore duplicate Zone error (#1) --- lib/zone.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/zone.ts b/lib/zone.ts index 7b10783de..4de4e077d 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -644,7 +644,21 @@ const Zone: ZoneType = (function(global: any) { } mark('Zone'); if (global['Zone']) { - throw new Error('Zone already loaded.'); + // if global['Zone'] already exists (maybe zone.js was already loaded or + // some other lib also registered a global object named Zone), we may need + // to throw an error, but sometimes user may not want this error. + // For example, + // we have two web pages, page1 includes zone.js, page2 doesn't. + // and the 1st time user load page1 and page2, everything work fine, + // but when user load page2 again, error occurs because global['Zone'] already exists. + // so we add a flag to let user choose whether to throw this error or not. + // By default, if existing Zone is from zone.js, we will not throw the error. + if (global[('__zone_symbol__forceDuplicateZoneCheck')] === true || + typeof global['Zone'].__symbol__ !== 'function') { + throw new Error('Zone already loaded.'); + } else { + return global['Zone']; + } } class Zone implements AmbientZone {