diff --git a/e2e/router/app/lazy/lazy.module.ts b/e2e/router/app/lazy/lazy.module.ts index 3f622e33a..311bdbfe4 100644 --- a/e2e/router/app/lazy/lazy.module.ts +++ b/e2e/router/app/lazy/lazy.module.ts @@ -1,7 +1,7 @@ import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core"; import { Route } from "@angular/router"; -import { NativeScriptModule } from "nativescript-angular/nativescript.module"; +import { NativeScriptCommonModule } from "nativescript-angular/common"; import { NativeScriptRouterModule } from "nativescript-angular/router"; import { LazyComponent } from "./lazy.component"; @@ -26,7 +26,7 @@ const routes: Route[] = [ @NgModule({ schemas: [NO_ERRORS_SCHEMA], imports: [ - NativeScriptModule, + NativeScriptCommonModule, NativeScriptRouterModule, NativeScriptRouterModule.forChild(routes) ], diff --git a/nativescript-angular/animations/animations.module.ts b/nativescript-angular/animations/animations.module.ts index 1a33c5daf..b8965c06f 100644 --- a/nativescript-angular/animations/animations.module.ts +++ b/nativescript-angular/animations/animations.module.ts @@ -1,4 +1,4 @@ -import { NgModule, Injectable, NgZone, Provider, RendererFactory2 } from "@angular/core"; +import { NgModule, Injectable, NgZone, Provider, RendererFactory2, Optional, SkipSelf } from "@angular/core"; import { AnimationBuilder } from "@angular/animations"; @@ -17,6 +17,7 @@ import { import { NativeScriptModule } from "../nativescript.module"; import { NativeScriptRendererFactory } from "../renderer"; import { NativeScriptAnimationDriver } from "./animation-driver"; +import { throwIfAlreadyLoaded } from "../common/utils"; @Injectable() export class InjectableAnimationEngine extends AnimationEngine { @@ -55,4 +56,8 @@ export const NATIVESCRIPT_ANIMATIONS_PROVIDERS: Provider[] = [ providers: NATIVESCRIPT_ANIMATIONS_PROVIDERS, }) export class NativeScriptAnimationsModule { + constructor(@Optional() @SkipSelf() parentModule: NativeScriptAnimationsModule) { + // Prevents NativeScriptAnimationsModule from getting imported multiple times + throwIfAlreadyLoaded(parentModule, "NativeScriptAnimationsModule"); + } } diff --git a/nativescript-angular/common/utils.ts b/nativescript-angular/common/utils.ts new file mode 100644 index 000000000..7d00c6f89 --- /dev/null +++ b/nativescript-angular/common/utils.ts @@ -0,0 +1,8 @@ +export function throwIfAlreadyLoaded( + parentModule: any, + moduleName: string, +) { + if ( parentModule ) { + throw new Error(`${moduleName} has already been loaded. Import ${moduleName} in the AppModule only.`); + } +} diff --git a/nativescript-angular/nativescript.module.ts b/nativescript-angular/nativescript.module.ts index 305890ab4..311c82227 100644 --- a/nativescript-angular/nativescript.module.ts +++ b/nativescript-angular/nativescript.module.ts @@ -14,11 +14,14 @@ import { NgModule, RendererFactory2, SystemJsNgModuleLoader, + Optional, + SkipSelf, } from "@angular/core"; import { NativeScriptCommonModule } from "./common"; import { NativeScriptRendererFactory } from "./renderer"; import { DetachedLoader } from "./common/detached-loader"; +import { throwIfAlreadyLoaded } from "./common/utils"; export function errorHandlerFactory() { return new ErrorHandler(); @@ -49,4 +52,8 @@ export function errorHandlerFactory() { schemas: [NO_ERRORS_SCHEMA] }) export class NativeScriptModule { + constructor(@Optional() @SkipSelf() parentModule: NativeScriptModule) { + // Prevents NativeScriptModule from getting imported multiple times + throwIfAlreadyLoaded(parentModule, "NativeScriptModule"); + } } diff --git a/tests/app/lazy-loaded.module.ts b/tests/app/lazy-loaded.module.ts index 13ac24300..601f1f79f 100644 --- a/tests/app/lazy-loaded.module.ts +++ b/tests/app/lazy-loaded.module.ts @@ -1,5 +1,5 @@ import { Component, NgModule } from "@angular/core"; -import { NativeScriptModule } from "nativescript-angular/nativescript.module"; +import { NativeScriptCommonModule } from "nativescript-angular/common"; import { NativeScriptRouterModule } from "nativescript-angular/router"; import { ModalDialogParams } from "nativescript-angular/directives/dialogs"; import { Page } from "ui/page"; @@ -31,7 +31,7 @@ export class ModalLazyComponent { ], entryComponents: [ModalLazyComponent], // when lazily loaded and opened via modal on demand imports: [ - NativeScriptModule, + NativeScriptCommonModule, NativeScriptRouterModule, NativeScriptRouterModule.forChild(routes) ],