1

I found this code for detecting the current route but it does not seem to work in Angular 7.

Here is the code:

app.component.ts

import { Routes } from '@angular/router';

...

constructor(private router: Routes) {}

app.component.html

<div *ngIf="router.url === '/someroute'">We are in someroute</div>

How can I check the route in the .html of a component in NG 7?

4 Answers 4

1

You have to inject Router instead of Routes

import { Router } from '@angular/router';

...

constructor(public router: Router) {}

and use it like

<div *ngIf="router.url === '/someroute'">We are in someroute</div>
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use it in the template, you must define it as public not private and use ActivatedRoute instead of Routes

so:

import { ActivatedRoute } from '@angular/router';

...

constructor(public router: ActivatedRoute) {}

Comments

0

Url is not available on Routes, You have to use Router

below is example,

import { Router } from '@angular/router';

constructor(private router: Router) {}

and in HTML,

<div *ngIf="router.url === '/someroute'">We are in someroute</div>

Comments

0

router must be of type Router Not Routes and in order to use it in the view template it should be public.

    import {  Router } from '@angular/router';
     .
     .
     .
     constructor(public router: Router) { }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.