Search code examples
angularangular-routing

How to config routerLink from component


Below routing working perfectly, but I wanted to handle the same functionality from component instead of HTML.

How do I do ?

HTML

<a [routerLink]="[ '..', card.item ]">VIEW MORE</a>

Solution

  • try this for relative navigation:

    constructor(private route: ActivatedRoute, private router: Router) {}
    
    moveTo() {
      this.router.navigate(['..', card.item], { relativeTo: this.route });
    }
    

    and add your click method in html

    <a (click)="moveTo()">VIEW MORE</a>