Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using routerLink and (click) in same button

Tags:

This is asking about a best practice. Is it a good practice to use routerLink and (click) in same button.

<button routerLink="/link" (click)="back()"> 

Because we can put the router navigation logic in the back() method, like below,

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

All I found regarding this is, this article which was not talking about the best practice to follow. If one is better than the other, can you explain the reason.

like image 704
dilantha111 Avatar asked May 11 '18 03:05

dilantha111


People also ask

Can I use routerLink on button?

Using Router linksAfter Angular v4 we can directly add a routerLink attribute on the anchor tag or button. Consider the following template, where routerLink attribute added to the anchor tag. The routerLink directive on the anchor tags give the router control over those elements.

Should I use href or routerLink?

Always avoid href when using Angular or any other SPA. routerLink loads resources internal to Angular, and the page that loaded in the browser is never completely reloaded (and so app state is maintained.)

What is the difference between routerLink and router navigate?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.


2 Answers

Following are few examples how you can play around with routerLink and click ,

Hope this will help :

-> If you want to redirect to certain pages you can always use this :

<a [routerLink]="['/my-book-listing']"> My Books</a> 

-> If you want to redirect to a page but want to send some paramater such as ID then use :

<a [routerLink]="['/my-book-details','3']">Book number 3</a> 

-> If there is a case in which you need to send Query params to route then you can use

<a [routerLink]="['/search-this']" [queryParams]="{text:'sam',category:'social'}">Go Search</a> 

-> If there is a need of code logic before navigating to the page then you need to use

<button (click)="createBook()">Create Book</button>  createBook(bookData){    // Create Book logic    this.router.navigate(['/my-book-listing']); } 

-> You can also use as follows but not a good practice unless you are calling function to destroy variables or save page leaving data

<button (click)="showLoader()" [routerLink]="['/my-book-listing']">Create Book</button>  showLoader(){   // showLoader logic } 
like image 199
Sangwin Gawande Avatar answered Oct 06 '22 00:10

Sangwin Gawande


If you need logic to occur before you go to a route you can import the Router Module and use it as such.

import { Component, OnInit } from '@angular/core' import { Router } from '@angular/router';  @Component({    selector: 'app-foo',    templateUrl: './foo.component.html',    styleUrls: ['./foo.component.sass'] }) export class FooComponent implements OnInit{    constructor( private router: Router ) { }    ngOnInit() {}     action(){       ...Your Logic...       this.router.navigate([ '/bar' ])    } } 
<div>    <button (click)='action()' >Go To Bar</button> </div> 
like image 20
Aundre Avatar answered Oct 06 '22 00:10

Aundre