Skip to content

Commit 50097c0

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 50097c0

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-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

+1-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,
@@ -19,16 +18,6 @@ import {
1918

2019
import { NativeScriptRendererFactory } from "./renderer";
2120
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";
3221

3322
export function errorHandlerFactory() {
3423
return new ErrorHandler(true);
@@ -37,32 +26,22 @@ export function errorHandlerFactory() {
3726
@NgModule({
3827
declarations: [
3928
DetachedLoader,
40-
ModalDialogHost,
41-
...NS_DIRECTIVES,
4229
],
4330
providers: [
44-
ModalDialogService,
4531
NativeScriptRendererFactory,
4632
SystemJsNgModuleLoader,
47-
defaultDeviceProvider,
48-
defaultFrameProvider,
49-
defaultPageProvider,
5033
{ provide: ErrorHandler, useFactory: errorHandlerFactory },
51-
{ provide: RendererFactory2, useClass: NativeScriptRendererFactory },
34+
{ provide: RendererFactory2, useExisting: NativeScriptRendererFactory },
5235
],
5336
entryComponents: [
5437
DetachedLoader,
5538
],
5639
imports: [
57-
CommonModule,
5840
ApplicationModule,
5941
],
6042
exports: [
61-
CommonModule,
6243
ApplicationModule,
6344
DetachedLoader,
64-
ModalDialogHost,
65-
...NS_DIRECTIVES,
6645
],
6746
schemas: [NO_ERRORS_SCHEMA]
6847
})

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)