nstudio / xplat

Cross-platform (xplat) tools for Nx workspaces.
MIT License
369 stars 52 forks source link

[Question] APP_INITIALIZER usage #250

Closed trunkovich closed 3 years ago

trunkovich commented 3 years ago

When you try to use APP_INITIALIZATION factory like it is described here:
https://nativescript.org/blog/angular-10-support/ You get an error:

NativeScript encountered a fatal error: Uncaught Error: Main entry is missing. App cannot be started. Verify app bootstrap.
at
ZoneAwareError(file: src/Users/trunkovich/work/projects/test/test-xplat-unit-tests/node_modules/@nativescript/angular/node_modules/@nativescript/zone-js/zone-nativescript.js:1298:0)
at createRootView(file: src/Users/trunkovich/work/projects/test/test-xplat-unit-tests/node_modules/@nativescript/core/application/index.js:312:0)
at setWindowContent(file: src/Users/trunkovich/work/projects/test/test-xplat-unit-tests/node_modules/@nativescript/core/application/index.js:257:0)
at notifyAppStarted(file: src/Users/trunkovich/work/projects/test/test-xplat-unit-tests/node_modules/@nativescript/core/application/index.js:168:0)
at didFinishLaunchingWithOptions(file: src/Users/trunkovich/work/projects/test/test-xplat-unit-tests/node_modules/@nativescript/core/application/index.js:151:0)
at push.../../../node_modules/@nativescript/core/application/index.js.NotificationObserver.onReceive(file: src/Users/trunkovich/work/projects/test/test-xplat-unit-tests/node_modules/@nativescript/core/application/index.js:41:0)

Steps to reproduce:

import { AppComponent } from "./app.component";

export function asyncBoot(): Function { return (): Promise => new Promise(resolve => { setTimeout(() => { console.log('asyncBoot'); resolve(); }, 30000); }) }

@NgModule({ bootstrap: [ AppComponent ], imports: [ NativeScriptModule ], declarations: [ AppComponent ], providers: [ { provide: APP_INITIALIZER, useFactory: asyncBoot, multi: true }, ], schemas: [ NO_ERRORS_SCHEMA ] }) export class AppModule { }

* also clean app.component from all unneeded staff:

import { Component } from '@angular/core';

@Component({ selector: 'test-xplat-unit-tests-root', template: `

<StackLayout class="page p-20">
  <Label text="'hello'"></Label>

` }) export class AppComponent {}


* `tns debug ios`

The same steps for the plain NS project work as expected.
NathanWalker commented 3 years ago

@trunkovich You may be missing the async option in your boot options. Here's the exact same setup on an example repo I setup showing just the changes you mentioned above but also ensuring the boot async option is there and all works just fine: https://github.com/NathanWalker/xplatdemo/commit/bd4fe7e1acee6bae9c19fdd81b8cd995fd0a8526

In particular this option in your main.ts:

platformNativeScriptDynamic({
  async: true // this must be set
}).bootstrapModule(AppModule);

You can also read about more advanced options here: https://nativescript.org/blog/angular-launch-animations/

trunkovich commented 3 years ago

@NathanWalker Thank you. It works now. Don't know why it works in plain NS project without this flag...