1

I want to redirect to another page by clicking on the link. For that I tried out below code:

home.component.html

<a [routerLink]="['/datalineage']">
Click Here
</a>

app-routing.module.ts

import { DatalineageComponent } from './datalineage/datalineage.component';

const appRoutes: Routes = [
    { path: 'datalineage', component: DatalineageComponent, data: { title: 'Test' } }
];

@NgModule({
    imports: [
        RouterModule.forRoot(
            appRoutes, { enableTracing: true }
        )
    ],
    exports: [
        RouterModule
    ],
    providers: []
})

datalineage.component.ts

import 'rxjs/add/operator/switchMap';
import { Observable } from 'rxjs/Observable';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';

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

  constructor() { }

  ngOnInit() {
  }
}

But it is not going to the clicked page, it shows http://localhost:4200/datalineage in URL but it remains on the same page. What can be the issue?

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98

1 Answers1

0

navigation template.
<h1>Working Routing</h1> <nav> <a [routerLink]=" ['./'] " routerLinkActive="active">Home</a> <a [routerLink]=" ['./datalineage'] " routerLinkActive="active">Data Lineage</a> </nav> <router-outlet></router-outlet>
app.routing template:

import { HomeComponent } from './home/home.component';
import { DatalineageComponent } from './datalineage/datalineage.component';

const appRoutes: Routes = [
    { path: 'home', component: HomeComponent },
    { path: 'datalineage', component: DatalineageComponent },
    { path: '', redirectTo: 'home' }
];

app.module template:

@NgModule({
    imports: [
        RouterModule.forRoot(appRoutes, { enableTracing: true })
    ],
    exports: [
        RouterModule
    ],
    providers: []
})

Reference: https://angular.io/guide/router

For how to pass data between components check these sources:

How do I pass data to Angular routed components?

https://angular.io/guide/component-interaction

mihai
  • 2,746
  • 3
  • 35
  • 56
  • what should be in `app.component.html`? – Pathik Vejani Mar 28 '18 at 06:37
  • it is not redirecting to `HomeComponent` if nothing is passed in url – Pathik Vejani Mar 28 '18 at 06:38
  • you can add the first snippet of code, under **navigation template** for now. Later you maybe will want to extract navigation in a separate component ;) – mihai Mar 28 '18 at 06:39
  • why it is not loading `HomeComponent`? – Pathik Vejani Mar 28 '18 at 06:40
  • "it is not redirecting to HomeComponent if nothing is passed in url " - it should, I set a `path: ''` which will handle the empty routes, otherwise... let me update the routing . 1 min – mihai Mar 28 '18 at 06:40
  • i got it but still it is not redirecting to home – Pathik Vejani Mar 28 '18 at 06:41
  • would you liek to check appRoutes snippet ? :) I've updated it. May it solves – mihai Mar 28 '18 at 06:42
  • can you remove what you have in app.component.html and paste just the snippet from **navigation template** section, just to ensure it is working. There is not a big problem with routing if you'll follow the instructions from the **Reference** I mentioned. – mihai Mar 28 '18 at 06:47
  • i removed and it shows pasted data of urs. so it is working but when i click on "home" link it is not redirecting. – Pathik Vejani Mar 28 '18 at 06:48