简体   繁体   English

在同一个按钮中使用 routerLink 和(单击)

[英]Using routerLink and (click) in same button

This is asking about a best practice.这是在询问最佳实践。 Is it a good practice to use routerLink and (click) in same button.在同一个按钮中使用 routerLink 和(单击)是一个好习惯。

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

Because we can put the router navigation logic in the back() method, like below,因为我们可以将路由器导航逻辑放在 back() 方法中,如下所示,

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.如果一个比另一个好,你能解释一下原因吗?

Following are few examples how you can play around with routerLink and click ,以下是一些如何使用routerLinkclick示例,

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 : -> 如果你想重定向到一个页面,但想发送一些参数,比如 ID,那么使用:

<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
}

I'm unsure of the best practice but I would say that it is fine to use routerLink and (click) in the same button as long as you do not interfere with the navigation.我不确定最佳做法,但我会说,只要不干扰导航,就可以在同一个按钮中使用 routerLink 和(单击)。

Manually navigation via this.router.navigate(['/link']);通过this.router.navigate(['/link']);手动导航this.router.navigate(['/link']); is sub-optimal since routerLink handles things like 'open in new tab' whereas implementing your own using a (click) handler is not desirable.是次优的,因为 routerLink 处理诸如“在新选项卡中打开”之类的事情,而使用(单击)处理程序实现您自己的处理程序是不可取的。

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>

I would go for routerLink when I simply have to navigate for example, to /link例如,当我只需要导航到/link时,我会选择routerLink

But if there is some logic which needs to be performed before navigating then I would go for click()但是如果在导航之前需要执行一些逻辑,那么我会去click()

Also there are cases when you have to pass in query parameters with routing navigation , then also I would prefer to use click()还有一些情况,你必须通过路由导航传递查询参数,那么我也更喜欢使用click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM