I have a home component within the homeModule and a contactUs component within the contactModule.
When I click cancel on the contactUs component, it should redirect me to the Home component.
Here are the routes:
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterModule, Routes} from '@angular/router';
import {homeComponent} from './home/home.component';
const routes: Routes = [
{ path: 'home', component: homeComponent},
{ path: 'demo-ui/home', component: homeComponent},
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [RouterModule],
declarations: []
})
export class AppHomeRoutingModule {
}
I attempted the following code but it did not work
import { Component, OnInit } from '@angular/core';
import { FormsModule, Validators, NgForm } from '@angular/forms';
import { Router } from '@angular/router';
@Component({
//some code
})
export class contactUsComponent implements OnInit {
constructor( private router: Router ){
}
ngOnInit() {
}
redirect(){
this.router.navigateByUrl('/home'); }
}
//html
<button class="btn btn-primary" type="button" (click)="redirect()">
Cancel </button>
I also tried
<button class="btn btn-primary" type="button" [routerLink]="['/home']"> Cancel </button>
and
<button class="btn btn-primary" type="button" routerLink="/home"> Cancel </button>
Error: Cannot match any routes. URL Segment './home'