2

I am trying to show a modal from a component that sits in a different module than the one where the modal sits. The modal is a navigation modal, so I first load the RootModalComponent that has a router <page-router-outlet></page-router-outlet> and onInit I redirect to the first modal. All this first part works well, but when I close the first modal, I get this error:

CONSOLE ERROR [native code]: ERROR Error: No componentRef found in DetachedRouteHandle

CONSOLE ERROR [native code]: ERROR CONTEXT {
"view": {
"def": {
...
"name": "page-router-outlet",
"attrs": [],
"template": null,
"componentProvider": null,
"componentView": null,
"componentRendererType": null,
"publicP

This error is triggered when I call the method below from the FirstModalComponent:

onClose(): void {
    this.modalParams.closeCallback();
}

The weirdest thing is that when I close the modal from the SecondModalComponent, everything is fine!

I Followed the Nativescript doc: https://docs.nativescript.org/ui/modal-view-ng to implement navigation with modal views, except that my router config is quite different and the RootModalComponent is not being called by a component from the same module.

markers-routing.Module.ts

const routes: Routes = [
    { path: 'first-modal', component: FirstModalComponent },
    { path: 'second-modal', component: SecondModalComponent },
];

@NgModule({
  imports: [NativeScriptRouterModule.forChild(routes)],
  exports: [NativeScriptRouterModule]
})
export class MarkersModalRoutingModule {}

markers.module.ts

@NgModule({
    declarations: [
        FirstModalComponent,
        SecondModalComponent,
        RootModalComponent
    ],
    imports: [
        NativeScriptCommonModule,
        NativeScriptFormsModule,
        MarkersModalRoutingModule
    ],
    schemas: [NO_ERRORS_SCHEMA],
    entryComponents: [
        RootModalComponent
    ]
})
export class MarkersModalModule { }

root-modal.component.ts

export class RootModalComponent implements OnInit {

    constructor(
        private router: RouterExtensions,
        private activeRoute: ActivatedRoute
    ) { }

    ngOnInit() {
        this.router.navigate(['first-modal'], { relativeTo: this.activeRoute });
    }

}

root-modal.component.html

<page-router-outlet></page-router-outlet>

Does someone know a workaround?

C00kieMonsta
  • 338
  • 3
  • 20
  • Can you share a Playground sample where the issue can be reproduced? – Manoj Mar 30 '19 at 17:16
  • Thanks for your help. Here you can find a playground: https://play.nativescript.org/?template=play-ng&id=vyv4kI&v=9 – C00kieMonsta Mar 30 '19 at 23:33
  • @Manoj I now have another error in the playground which I can't fix either, it says `ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[FirstModalComponent -> ModalDialogParams]`! I don't have this error in my project... – C00kieMonsta Mar 31 '19 at 13:04
  • I didn't find time to fix the Playground but to fix that issue, you must include `ModalDialogService` in `providers`. Besides having a quick look at your code, I don't think your modal dialogs can be lazily loaded. It has to be part of your home module or app module. – Manoj Mar 31 '19 at 13:13
  • @Manoj adding the `ModalDialogService` in `providers` did not work as you can see on this playground: https://play.nativescript.org/?template=play-ng&id=vyv4kI&v=11 ! Additionally, when I put all components inside the AppModule, the same error is triggered... `No componentRef found in DetachedRouteHandle` – C00kieMonsta Mar 31 '19 at 13:53

2 Answers2

2

Currently, we need a workaround to make it work because there is a bug in the nativescript angular framework (see issue 1774).

Basically, the workaround consists in naming the page-router-outlet inside the root-modal.component.html (in the example below I named it sharedModal). You then use this name to navigate to the other modals.

For example:

this.router.navigate([{ outlets: { sharedModal: ["first-modal"] } }], { relativeTo: this.activeRoute });

Find here a fully developed nativescript playground sample: https://play.nativescript.org/?template=play-ng&id=TkK7sQ&v=5

C00kieMonsta
  • 338
  • 3
  • 20
1

First of all you should include ModalDialogService in providers. Also I don't think you could load modal dialog routes lazily, it should be part of your current module (in your case HomeModule). You don't have to import HomeModule in app module as it's being loaded lazily from app routing.

Updated Playground

Manoj
  • 21,753
  • 3
  • 20
  • 41
  • Thanks for your help, but I still get the `ModalDialogService` error and concerning the modals, I added all components to the `HomeModule` and I still get the same error `No componentRef found in DetachedRouteHandle`... this is really weird – C00kieMonsta Mar 31 '19 at 22:48
  • 1
    Which device you are testing it with? It works perfectly fine on my end, please make sure the Playground version is correct one (v12). – Manoj Mar 31 '19 at 22:50
  • Sorry... apparently I was not using the proper version, my bad. Could be that the version on my preview app was stuck on an older version, it works well now. Unfortunatley I can't recreate the bug I have... thanks for your help touhg – C00kieMonsta Mar 31 '19 at 22:55
  • Did you already get an error like this `No component factory found for RootModalComponent. Did you add it to @NgModule.entryComponents?` while the component is still inside the entryComponents of the `HomeModule`? – C00kieMonsta Mar 31 '19 at 23:27
  • Would it be possible that all of this is caused because I am achieving all of this inside a tab component? – C00kieMonsta Mar 31 '19 at 23:37
  • 1
    I didn't get any error, the version I have shared works for me, I was able to navigate within modal pages. Using them inside Tab has nothing to do with it. – Manoj Apr 01 '19 at 05:07
  • I created a new Playground where I added a tab navigation, now there is an error with the url segment that is not recognized: https://play.nativescript.org/?template=play-ng&id=NRmf4z and the error is `Cannot match any routes. URL Segment: 'first-modal'`... any idea? – C00kieMonsta Apr 01 '19 at 22:06
  • In youe sample RootModalComponent is missing – Narendra Apr 02 '19 at 01:57
  • @Narendra Sorry I added the wrong playground, this is the one: https://play.nativescript.org/?template=play-ng&id=VdmiR9&v=8 – C00kieMonsta Apr 02 '19 at 16:40