Skip to content

Commit e308645

Browse files
committed
refactor: separate NativeScriptCommonModule from NativeScriptModule
All common providers, such as CommonModule (from @angular/common), Frame, Page, Device and Modals are moved from NativeScriptModule to NativeScriptCommonModule. It can be required from "nativescript-angular/common". The NativeScriptRouterModule also imports the common module now. Importing NativeScriptModule more than once causes reinstantiating of the NativeScriptRenderer, which breaks animations. BREAKING CHANGES: NativeScriptModule should be imported only in the root application module (usually named AppModule). It provides essential the Renderer, ModuleLoader and exports the ApplicationModule. All other NgModules in the app (both feature and lazy-loaded ones) should import the NativeScriptCommonModule instead. The behaviour is alligned with BrowserModule and CommonModule in web Angular apps. https://angular.io/guide/ngmodule-faq#q-browser-vs-common-module
1 parent b3cd887 commit e308645

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

Diff for: nativescript-angular/common.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { CommonModule } from "@angular/common";
2+
import {
3+
NO_ERRORS_SCHEMA,
4+
NgModule,
5+
} from "@angular/core";
6+
7+
import {
8+
ModalDialogHost,
9+
ModalDialogService,
10+
} from "./directives/dialogs";
11+
import {
12+
defaultDeviceProvider,
13+
defaultFrameProvider,
14+
defaultPageProvider,
15+
} from "./platform-providers";
16+
import { NS_DIRECTIVES } from "./directives";
17+
18+
@NgModule({
19+
declarations: [
20+
ModalDialogHost,
21+
...NS_DIRECTIVES,
22+
],
23+
providers: [
24+
ModalDialogService,
25+
defaultDeviceProvider,
26+
defaultFrameProvider,
27+
defaultPageProvider,
28+
],
29+
imports: [
30+
CommonModule,
31+
],
32+
exports: [
33+
CommonModule,
34+
ModalDialogHost,
35+
...NS_DIRECTIVES,
36+
],
37+
schemas: [NO_ERRORS_SCHEMA]
38+
})
39+
export class NativeScriptCommonModule {
40+
}

Diff for: nativescript-angular/nativescript.module.ts

+4-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import "reflect-metadata";
77
import "./polyfills/array";
88
import "./polyfills/console";
99

10-
import { CommonModule } from "@angular/common";
1110
import {
1211
ApplicationModule,
1312
ErrorHandler,
@@ -17,18 +16,9 @@ import {
1716
SystemJsNgModuleLoader,
1817
} from "@angular/core";
1918

19+
import { NativeScriptCommonModule } from "./common";
2020
import { NativeScriptRendererFactory } from "./renderer";
2121
import { DetachedLoader } from "./common/detached-loader";
22-
import {
23-
ModalDialogHost,
24-
ModalDialogService,
25-
} from "./directives/dialogs";
26-
import {
27-
defaultDeviceProvider,
28-
defaultFrameProvider,
29-
defaultPageProvider,
30-
} from "./platform-providers";
31-
import { NS_DIRECTIVES } from "./directives";
3222

3323
export function errorHandlerFactory() {
3424
return new ErrorHandler(true);
@@ -37,32 +27,24 @@ export function errorHandlerFactory() {
3727
@NgModule({
3828
declarations: [
3929
DetachedLoader,
40-
ModalDialogHost,
41-
...NS_DIRECTIVES,
4230
],
4331
providers: [
44-
ModalDialogService,
4532
NativeScriptRendererFactory,
4633
SystemJsNgModuleLoader,
47-
defaultDeviceProvider,
48-
defaultFrameProvider,
49-
defaultPageProvider,
5034
{ provide: ErrorHandler, useFactory: errorHandlerFactory },
51-
{ provide: RendererFactory2, useClass: NativeScriptRendererFactory },
35+
{ provide: RendererFactory2, useExisting: NativeScriptRendererFactory },
5236
],
5337
entryComponents: [
5438
DetachedLoader,
5539
],
5640
imports: [
57-
CommonModule,
5841
ApplicationModule,
42+
NativeScriptCommonModule,
5943
],
6044
exports: [
61-
CommonModule,
6245
ApplicationModule,
46+
NativeScriptCommonModule,
6347
DetachedLoader,
64-
ModalDialogHost,
65-
...NS_DIRECTIVES,
6648
],
6749
schemas: [NO_ERRORS_SCHEMA]
6850
})

Diff for: nativescript-angular/router.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { PageRouterOutlet } from "./router/page-router-outlet";
1414
import { NSLocationStrategy, LocationState } from "./router/ns-location-strategy";
1515
import { NativescriptPlatformLocation } from "./router/ns-platform-location";
1616
import { RouterExtensions } from "./router/router-extensions";
17-
import { NativeScriptModule } from "./nativescript.module";
17+
import { NativeScriptCommonModule } from "./common";
1818

1919
export { PageRoute } from "./router/page-router-outlet";
2020
export { RouterExtensions } from "./router/router-extensions";
@@ -40,7 +40,7 @@ export type LocationState = LocationState;
4040
],
4141
imports: [
4242
RouterModule,
43-
NativeScriptModule
43+
NativeScriptCommonModule,
4444
],
4545
exports: [
4646
RouterModule,

0 commit comments

Comments
 (0)