Exploring Ionic 4 with Angular Router

Presently, I am working on developing an application using the latest beta version 4 of Ionic and implementing the tabs layout.

I am still trying to grasp how the navigation works with the new Angular router.

This is my app-routing.module.ts:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  { path: 'welcome', loadChildren: './pages/welcome/welcome.module#WelcomePageModule' },
  { path: 'login', loadChildren: './pages/auth/login/login.module#LoginPageModule' },
  { path: 'registration', loadChildren: './pages/auth/registration/registration.module#RegistrationPageModule' },
  { path: 'app', loadChildren: './pages/tabs/tabs.module#TabsPageModule'},
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {}

And this is my tabs.router.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { TabsPage } from './tabs.page';
import { ContactPage } from '../contact/contact.page';
import { EventOverviewPage } from '../event-overview/event-overview.page';
import { EventDetailPage } from '../event-detail/event-detail.page';
import { ProfilPage } from '../profil/profil.page'


const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'eventOverview',
        outlet: 'eventOverview',
        component: EventOverviewPage,
      },
      {
        path: 'event/:eventId',
        component: EventDetailPage,
        outlet: 'eventOverview'
      },
      {
        path: 'profil',
        outlet: 'profil',
        component: ProfilPage
      },
      {
        path: 'contact',
        outlet: 'contact',
        component: ContactPage
      }
    ]
  }
];

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

When using

router.navigateByUrl('/app/tabs/(eventOverview:eventOverview)')
, I'm able to navigate to the Overview page and access the eventDetail page with a custom id using:

this.router.navigateByUrl('app/tabs/(eventOverview:event/${id})')

Currently, my challenge lies in passing more than one parameter to the EventDetailPage. I read that this can be achieved with the router.navigate([]) function, so I attempted:

this.router.navigate(['/app/tabs/eventOverview'], { queryParams: { eventId: eventId} });

and

this.router.navigate(['/app/tabs/(eventOverview:event)'], { queryParams: { eventId: eventId} });

However, I consistently encounter an error when trying to navigate to the EventDetailsPage:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'app/tabs' Error: Cannot match any routes. URL Segment: 'app/tabs'

It appears that I haven't fully grasped how the routing system functions.

If anyone could provide some guidance, it would be greatly appreciated.

//edit:

Here is an example on StackBlitz: https://stackblitz.com/edit/github-ionic4navigation-emxydw

It is possible to navigate to eventDetails by clicking an item on the list from the start screen for the first time. However, upon returning and attempting again, it no longer works.

Additionally, I am unable to find a way to navigate from the create-event.page to the eventDetails.page.

Answer №1

Have you attempted the following steps?

this.router.navigate(['/app/pages/tabs/event', { eventId: eventId}]);

This specific route, path: 'event/:eventId',, is the only one that can support a query parameter.

You may find it beneficial to refer to this article for more insights on angular routing with ionic:

Answer №2

It seems like the issue is stemming from a lack of clarity on which route to take...

EDIT

I spent some time investigating this last night, and I believe your code is heading in the right direction. The problem might lie in the organization or structure of your routing modules. While the initial pattern seems correct, it appears to get lost in the tabs.router.module.ts file. This could be due to references to components intended for other routing modules, such as event-overview-routing.module.ts and event-detail-routing.module.ts.

 children: [
      {
        path: 'eventOverview',
        outlet: 'eventOverview',
        component: EventOverviewPage,
      },
      {
        //path: 'event/:eventId',
        path: 'eventOverview/eventDetails',
        outlet: 'eventOverview',
        component: EventDetailPage,
        children: [
          {
            path: ':id',
            component: EventDetailPage
          },
           {
            path: ':id/:Status',
            component: EventDetailPage
          }
        ]
      },
      {
        path: 'profil',
        outlet: 'profil',
        component: ProfilPage
      }

If you intend to navigate to your EventDetailPage, ensure you match the path path: 'event/:eventId'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for correctly setting object initial values in React CreateContext

How can I correctly define the initial value of the constance trainsDetails in React Create Context? The trainsDetails is an object with various properties, fetched as a single object from an endpoint and has the values specified below in the TrainsDetails ...

LeafletJS: The map container has already been initialized

In my Ionic 2 app, I am utilizing Leaflet. The app works perfectly fine when launched for the first time. However, if I navigate to another page and then return to the map page, I encounter the following exception: ERROR: Error: Uncaught (in promise): ERR ...

What's Causing the UNMET PEER DEPENDENCY Error in Angular 8 with @angular/[email protected]?

After updating to the latest version of Angular 8, everything seems to be working fine without any issues. However, I am seeing some strange messages when running npm list after completing npm install: UNMET PEER DEPENDENCY @angular/<a href="/cdn-cgi/ ...

The 'import.meta' meta-property can only be used with the '--module' set to 'es2020', 'esnext', or 'system'.ts(1343)

Whenever I attempt to utilize import.meta.url (as demonstrated in the Parcel docs), I am consistently met with the error message "The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es ...

Tips on updating TypeScript to a higher major version

Despite upgrading all packages, deleting node_modules and package-lock.json, and reinstalling with npm install, the typescript runtime in my git repo is still showing version 4.9.5. How can I update the tsc version to be higher than 5.0? $ npx tsc --versi ...

Function type guards in Typescript do not support type inference

When checking for null in alpha, I validate the result and use throw new Error if needed. However, even after doing so, the compiler still indicates a compilation error: const obj = { objMethod: function (): string | null { return 'always a str ...

Associate a unique identifier string with a randomly generated integer identifier by Agora

For my current web project, I am utilizing a String username as the UID to connect to the channel in an Agora video call. However, I now need to incorporate individual cloud recording by Agora into the project. The challenge lies in the fact that cloud r ...

Retrieve a specific number from an equation

With my limited knowledge of TypeScript, I am attempting to extract a specific number from an expression. The goal is to locate and retrieve the digit from the following expression. ID:jv.link.weight:234231 In the given string, I aim to extract the numb ...

NPM IP library susceptible to Server-Side Request Forgery (SSRF) vulnerabilities

Received Security Alert from GitHub's Dependabot Regarding an Issue in My Angular Repository A security vulnerability has been identified in all versions of the NPM package "ip," allowing a malicious actor to execute arbitrary code and access sensiti ...

Encountering a problem while trying to integrate ng-zorro-antd in an Angular 8 project using the CLI command ng add ng-zorro

I am encountering an issue when trying to integrate ng-zorro-antd with Angular 8 using the CLI. I have followed these steps: Create a new project using 'ng new PROJECT_NAME' Navigate into the project directory using 'cd PROJECT_NAME' ...

Is it possible to assign a type conditionally depending on the value of a boolean?

While grappling with this issue, the title question arose in my mind: How can I handle the situation where the library function getResponse returns { a: number } for Android phones and { b: number } for iOS phones? The code snippet below initially led to ...

Typescript indicates that an object may be potentially null

I've hit a roadblock where I keep getting warnings that the objects might be null. After searching online and on StackOverflow, I've tried numerous solutions with no luck. My goal is to insert the text "test" into the HTML elements using their ID ...

Tips for displaying both the time and date using Angular Material Date Picker

I recently integrated the Angular Material date picker control into my Angular project. I want to display both the date and time together. Could someone please advise me on how I can achieve this using the Angular Material date picker control? Appreciate ...

Using aliases in npm packages is not supported

I am working on creating an npm package that I want to use in another application. During development, I set a path in tsconfig for importing various modules instead of using a relative path. However, when I download my package into the test app, it is una ...

Issue with directive implementation of regex test as attribute - validations in typescript and angular

I am currently working on a project to create a module with custom validation directives. These validations are implemented using regular expressions (regex). The specific error I encountered is: Error: [$injector:unpr] Unknown provider: REG_EXPProvid ...

Utilizing Conditional CSS Classes in React Material-UI (MUI) 5

I am in the process of migrating from React material-ui 4 to MUI 5. How can I implement this particular design pattern using the new styled API (or any other suitable method)? My project is written in Typescript. const useStyles = makeStyles(theme => ...

Implementing secure access control in Angular based on user roles

Currently, we are working on integrating role-based security with Angular. However, we are looking for a solution to implement this on the server side to prevent users from accessing the page by manipulating JavaScript code in their browsers. While our se ...

Verifying the legitimacy of the elements in an n-dimensional array

In my journey to create my own Tensor class (n-dimensional arrays) in typescript, I have devised a structure where the data is stored in a 1D array property, and the shape of the data is stored separately for indexing purposes. My goal is to develop a fun ...

Unable to successfully import { next } from the 'express' module using Typescript

Having some trouble with this line of code: import {response, request, next} from 'express' The typescript compiler in vscode is giving me the following error: Module '"express"' has no exported member 'next'. Up ...

TypeScript code runs smoothly on local environment, however encounters issues when deployed to production

<div> <div style="text-align:center"> <button class="btnClass">{{ submitButtonCaption }}</button> <button type="button" style="margin-left:15px;" class="cancelButton" (click)="clearSearch()"> {{ clearButtonCapt ...