angular routing using router.navigate

Anjali

I am trying to navigate to another component from my current component like so:

this.router.navigate(['/relationshipInfo']);

The above statement is not routing to a different component, Below is my code in app-routing.module.ts:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {PersonalInfoComponent} from './PersonalInfo/PersonalInfo.component';
import {RequiredInfoComponent} from './RequiredInfo/RequiredInfo.component';
import {RelationshipInfoComponent} from './relationshipInfo/relationshipInfo.component'

const routes: Routes = [

  {path: 'PersonalInfo', component: PersonalInfoComponent},
  {
    path: '',
    children: [
      {path: 'RequiredInfo', component: RequiredInfoComponent},
      {path: 'RelationshipInfo', component: RelationshipInfoComponent},

  ]

  },

  {path: '**', redirectTo: 'PersonalInfo', pathMatch: 'full'},

];

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

below is my app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { PersonalInfoComponent } from './PersonalInfo/PersonalInfo.component';
import { MaterialModule } from './shared/Material.Module';
import { FormsModule } from '@angular/forms';
import { RequiredInfoComponent } from './RequiredInfo/RequiredInfo.component';
import { RelationshipInfoComponent } from './relationshipInfo/relationshipInfo.component';

@NgModule({
   declarations: [
      AppComponent,
      PersonalInfoComponent,
      RequiredInfoComponent,
      RelationshipInfoComponent
   ],
   imports: [
      BrowserModule,
      AppRoutingModule,
      MaterialModule,
      FormsModule
   ],
   providers: [],
   bootstrap: [
      AppComponent
   ]
})
export class AppModule { }

I am trying to route on the click of my button. the button exists in PersonalInfo page. Below is the HTML and .ts code:

Online

and below is my .ts code:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-PersonalInfo',
  templateUrl: './PersonalInfo.component.html',
  styleUrls: ['./PersonalInfo.component.css']
})
export class PersonalInfoComponent implements OnInit {
  public Loaded = false;
  public btnshow=true;
  constructor(private router:Router) { }

  ngOnInit() {
  }

  onlinePage(){
    this.router.navigate(['/RelationshipInfo']);
    this.router.navigateByUrl('/RelationshipInfo');
  }

  Continue()
  {
    //this.router.navigate(['/RequiredInfo']);
    this.router.navigateByUrl('/RequiredInfo');

  }

  Cancel(){}

}

any help will be greatly appreciated.

alexortizl

Angular routes path are case sensitive. You have you route defined as:

{path: 'RelationshipInfo', component: RelationshipInfoComponent},

with capital R so this.router.navigate should also be with capital R

this.router.navigate(['/RelationshipInfo']);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

router.navigate with query params Angular 5

Angular 2 router.navigate

Navigate relative with Angular 2 Router (Version 3)

Programmatically navigate using react router

Angular 2 - How to navigate to another route using this.router.parent.navigate('/about')?

Angular Navigate Routing between parent and child

Conditionally navigate to state with angular ui-router

Component constructor and Router life-cycle hooks not triggered when calling router.parent.navigate method from child component in Angular2 routing

Angular - Router navigate canceled without reason

Angular router.navigate() delta

Angular 5 router.navigate does not work

Angular 2 Routing navigate run in new tab(Use Angular Router naviagte )

Router navigate to child route - Angular 6

Angular4 router breaks after using `router.navigate()` in Google Sign In callback

Angular Change Detection not triggering on Router.navigate()

Angular 2 redirecting automatically using router.navigate

Router Navigate Using CreateUrlTree

automatic navigate using routing in Nativescript

Angular ui routing navigate with parameters

angular router navigate then reload

Router navigate on Ionic with animation blinking on ios using angular animations

Using angular-router to navigate views within a page

angular 7 routing fails when using named router-outlet

Why is Angular router.navigate() not navigating?

Unable to navigate using router.nagivate in angular2

Angular2 router and navigate

Using the angular-ui-router to navigate from ionic footers

Router not resolving navigate Angular with AngularFirestore returns

karma angular test router navigate