Navigating Ionic 5 using Angular from within sub component

Viewed 209

I have a project set up where I have a main page that displays different components inside of it. However, when I try to navigate using a button inside of the subcomponent, the button does not navigate. The desired effect is as so:

login --> main/child --> other-page

where the button to navigate resides inside the child component not the page that the component is in.

HTML

login.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>login</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <ion-button routerLink='/main/child1'>Login</ion-button>
</ion-content>

main.page.html

<ion-content>
  <span> <!-- this span is a menu -->
    <ion-button routerLink="/main/profile">Profile</ion-button>
    <ion-button routerLink="/main/child1">Child 1</ion-button>
    <ion-button routerLink="/main/child2">Child 2</ion-button>
  </span>

  <router-outlet></router-outlet>
  
  <ion-button routerLink="/other-page">Other Page</ion-button>
  <!-- Navigates Properly but not wanted here-->
</ion-content>

child1.component.html

<ion-content>
  Child 1
  <ion-button routerLink='/other-page'>Other Page</ion-button>
  <!-- Navigation Doesn't Work-->
</ion-content> 

Routing Modules

app-routing.module.ts

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

const routes: Routes = [
  {
    path: '',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: 'login',
    loadChildren: () => import('./pages/login/login.module').then( m => m.LoginPageModule)
  },
  {
    path: 'main',
    loadChildren: () => import('./pages/main/main.module').then( m => m.MainPageModule)
  },
  {
    path: 'other-page',
    loadChildren: () => import('./pages/other-page/other-page.module').then( m => m.OtherPagePageModule)
  },
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

login-routing.module.ts

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

import { LoginPage } from './login.page';

const routes: Routes = [
  {
    path: '',
    component: LoginPage
  }
];

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

main.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Child1Component } from 'src/app/components/child1/child1.component';
import { Child2Component } from 'src/app/components/child2/child2.component';
import { ProfileComponent } from 'src/app/components/profile/profile.component';


import { MainPage } from './main.page';

const routes: Routes = [
  {
    path: '',
    redirectTo: 'main/child1',
    pathMatch: 'full'
  },
  {
    path: '',
    component: MainPage,
    children: [
      {
        path: 'child1',
        component: Child1Component
      },
      {
        path: 'child2',
        component: Child2Component
      },
      {
        path: 'profile',
        component: ProfileComponent
      },
    ]
  },
  
];

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

other-routing.module.ts

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

import { OtherPagePage } from './other-page.page';

const routes: Routes = [
  {
    path: '',
    component: OtherPagePage
  }
];

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

Thank you ahead of time for everyone who helps.

0 Answers
Related