Skip to content

Instantly share code, notes, and snippets.

@vchimev
Forked from vakrilov/livesync-navigation.ts
Last active February 7, 2019 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vchimev/070882801470d203ca7840b731cb175e to your computer and use it in GitHub Desktop.
Save vchimev/070882801470d203ca7840b731cb175e to your computer and use it in GitHub Desktop.
NativeScript-Angular HMR
import { NgZone } from "@angular/core";
import { Router } from "@angular/router";
import { onBeforeLivesync, onAfterLivesync } from "nativescript-angular/platform-common";
import { RouterExtensions } from "nativescript-angular/router";
let cachedUrl: string;
onBeforeLivesync.subscribe(moduleRef => {
console.log("#### onBeforeLivesync");
if (moduleRef) {
const router = <Router>moduleRef.injector.get(Router);
cachedUrl = router.url;
console.log("-------> Caching URL: " + cachedUrl);
}
});
onAfterLivesync.subscribe(({ moduleRef, error }) => {
console.log(`#### onAfterLivesync moduleRef: ${moduleRef} error: ${error}`);
if (moduleRef) {
const router = <RouterExtensions>moduleRef.injector.get(RouterExtensions);
const ngZone = <NgZone>moduleRef.injector.get(NgZone);
if (router && cachedUrl) {
ngZone.run(() => { // <-- should be wrapped in ngZone
router.navigateByUrl(cachedUrl, { animated: false });
});
}
}
});
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppOptions } from "nativescript-angular/platform-common";
import { AppModule } from "./app/app.module";
// Optional - attach to livesync hooks and perfrom navigation
import "./livesync-navigation";
let options: AppOptions = {};
if (module["hot"]) {
options.hmrOptions = {
moduleTypeFactory: () => AppModule,
livesyncCallback: (platformReboot) => {
setTimeout(platformReboot, 0);
},
};
// Path to your app module.
// You might have to change it if your module is in a different place.
module["hot"].accept(["./app/app.module"], () => {
global["hmrRefresh"]();
});
}
// Don't forget to pass the options when creating the platform
platformNativeScriptDynamic(options).bootstrapModule(AppModule);
@edwinquaihoi
Copy link

Import on line 4 should be import { AppModule } from "./app/app.module";

@vchimev
Copy link
Author

vchimev commented Feb 7, 2019

Thank you, @edwinquaihoi! I updated the import. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment