0
votes

I'm developing a mobile app with nativescript + Angular 8. Basically the app download a fat json object from the backend and then with multiple nested routing give the possibility to select a specific action.

The routing chain (each moduel is the child of the previous) is written below: app-routing.module.ts -> main-routing.module.ts -> asset-routing.module.ts -> future-state.routing.module.ts

in the main.component.html is rendered a list of selectable assets generated dynamically from the json downloaded. When an asset is clicked a new list of selectable future-state generated dynamically from the json for the specific asset is shown. When a future-state is selected a list of selectable activities generated dynamically from the json for the specific future-state is shown. At the end the user can select the activity and the data is sent back to the backend.

In the selection phase everything is ok until the future-state item selection. When I try to select a future-state the code return me the Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: ....

future-state.commponent.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { RouterExtensions } from "nativescript-angular/router";
import { GlobalVariablesService } from '../../../_services/global-variables.service';
import { AndonPossibleIssueOpenStateDto } from '../../../_models/andon-possible-issue-open-state-dto';

@Component({
  selector: 'ns-future-state',
  templateUrl: './future-state.component.html',
  styleUrls: ['./future-state.component.css']
})
export class FutureStateComponent implements OnInit {

  id: number;
  andonPossibleIssueOpenStateDto: AndonPossibleIssueOpenStateDto;

  constructor(
    private globalVariables: GlobalVariablesService,
    private _route: ActivatedRoute,
    private _routerExtensions: RouterExtensions
  ) { }

  ngOnInit(): void {
    const idFutureState = +this._route.snapshot.params.id;
    this._route.params.subscribe(params => { this.id = params['id']; });
    //this.andonPossibleIssueOpenStateDto=this.globalVariables.getAndonBotAssetPossibleStateIssueDto().find(function(element){return element.asset.id==idAsset}) as AndonPossibleIssueOpenStateDto;
  }

  onBackTap(): void {
        this._routerExtensions.back();
    }

}

future-state.module.ts

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptCommonModule } from "nativescript-angular/common";

import { FutureStateRoutingModule } from "./future-state-routing.module";
import { FutureStateComponent } from "./future-state.component";
import { ActivityComponent } from "./activity/activity.component";

@NgModule({
    imports: [
        NativeScriptCommonModule,
        FutureStateRoutingModule
    ],
    declarations: [
        FutureStateComponent,
        ActivityComponent
    ],
    exports: [FutureStateComponent],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
export class FutureStateModule { }

future-state-routing.module.ts

import { NgModule } from "@angular/core";
import { Routes } from "@angular/router";
import { NativeScriptRouterModule } from "nativescript-angular/router";

import { FutureStateComponent } from "./future-state.component";
import { ActivityComponent } from "./activity/activity.component";

const routes: Routes = [
    { 
        path: "", 
        component: FutureStateComponent 
    },
    { 
        path: "activity/:id", 
        component: ActivityComponent 
    }
];

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

asset.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { RouterExtensions } from "nativescript-angular/router";
import { GlobalVariablesService } from '../../_services/global-variables.service';
import { AndonBotAssetPossibleStateIssueDto } from '../../_models/andon-bot-asset-possible-state-issue-dto';


@Component({
  selector: 'ns-asset',
  templateUrl: './asset.component.html',
  styleUrls: ['./asset.component.css']
})
export class AssetComponent implements OnInit {

  id: number;
  andonBotAssetPossibleStateIssueDto: AndonBotAssetPossibleStateIssueDto;

  constructor(
    private globalVariables: GlobalVariablesService,
    private _route: ActivatedRoute,
    private _routerExtensions: RouterExtensions
  ) { }

  ngOnInit(): void {
    const idAsset = +this._route.snapshot.params.id;
    this.id = this._route.snapshot.params['id'];
    console.log(this._route.snapshot.children);
    console.log(this._route.snapshot.routeConfig);
    this.andonBotAssetPossibleStateIssueDto=this.globalVariables.getAndonBotAssetPossibleStateIssueDto().find(function(element){return element.asset.id==idAsset}) as AndonBotAssetPossibleStateIssueDto;
    console.log(this.andonBotAssetPossibleStateIssueDto.asset);
    console.log(this._route.firstChild);
  }

  onBackTap(): void {
        this._routerExtensions.back();
    }

}

asset.coponent.html

<ActionBar class="action-bar">
    <Label class="action-bar-title" [text]="andonBotAssetPossibleStateIssueDto.asset.name"></Label>
</ActionBar>

<StackLayout class="page page-content">
    <ListView [items]="andonBotAssetPossibleStateIssueDto.possibleStateIssueDto.optionStateIssue" class="list-group">
        <ng-template let-item="item">
            <Label [nsRouterLink]="['./futureState', item.futureState.id]" [text]="item.futureState.name" class="list-group-item"></Label>
        </ng-template>
    </ListView>
</StackLayout>

asset.module.ts

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptCommonModule } from "nativescript-angular/common";

import { AssetRoutingModule } from "./asset-routing.module";
import { AssetComponent } from "./asset.component";
import { FutureStateModule } from "./future-state/future-state.module";

@NgModule({
    imports: [
        NativeScriptCommonModule,
        AssetRoutingModule,
        FutureStateModule
    ],
    declarations: [
        AssetComponent
    ],
    exports: [AssetComponent],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
export class AssetModule { }

asset-routing.module.ts

import { NgModule } from "@angular/core";
import { Routes } from "@angular/router";
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { NSEmptyOutletComponent } from "nativescript-angular";

import { AssetComponent } from "./asset.component";
import { FutureStateComponent } from "./future-state/future-state.component";

const routes: Routes = [
    {
        path: "",
        component: AssetComponent
    },
    {
        path: "futureState/:id",
        component: FutureStateComponent
    },
    {
        path: "futureState",
        component: NSEmptyOutletComponent,
        loadChildren: () => import("~/app/main/asset/future-state/future-state.module").then((m) => m.FutureStateModule)
    }
];

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

Below the error message:

Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'main/asset/698/futureState/6'

JS: Error: Cannot match any routes. URL Segment: 'main/asset/698/futureState/6'

JS:
at ApplyRedirects.push.../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.noMatchError (file:///node_modules@angular\router\fesm5\router.js:2459:0) [angular]
JS: at CatchSubscriber.selector (file:///node_modules@angular\router\fesm5\router.js:2440:0) [angular]
JS: at CatchSubscriber.push.../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error (file:///node_modules\rxjs_esm5\internal\operators\catchError.js:34:0) [angular] JS: at MapSubscriber.push.../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._error (file:///node_modules\rxjs_esm5\internal\Subscriber.js:79:0) [angular] JS: at MapSubscriber.push.../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.error (file:///data/data/org.nativescript.AndonMobile/fil...

1

1 Answers

0
votes

The issue was that in the asset.component.html the link requested was /main/asset/[idAsset]/futureState/[idFutureState] instead the router dynamically generated in asset-routing.module.ts was something like /main/asset/futureState/[idFutureState].

I've modified the part:

 {
    path: "futureState/:id",
    component: FutureStateComponent
 }

with:

{
   path: ":this.id/futureState/:id",
   component: FutureStateComponent
}