href in <a> tag vs window.open in Angular



使用hrefwindow.open有哪些缺点。我只想让用户在Angular应用程序中点击链接后重定向到另一个页面。

E.g

<a href="www.google.com" target="_blank">Google</a>

<a (click)="redirectMe()">Google</a>

TS

redirectMe() {
window.open('www.google.com','_blank');
}

使用href是直接重定向到新页面,而在方法中使用window.open((等函数可能需要在重定向之前验证某些内容。

很多因素都起了作用,对我来说,最好使用href,因为它需要更少的代码,只需要html,而且不需要JavaScript

请注意,在这种情况下,标签的head或target属性中的标签是一个相关元素,用于定义在新页面或同一页面中重定向的方式。

<a href="url">name<a>:将带您访问同一浏览器选项卡上的url

window.open('url'):将在新的浏览器选项卡中打开页面

选项1-如果你只是重定向到已知的url,你可以很好地使用,如果你想在新选项卡中打开,请使用target="_blank"-

<a href="www.google.com">Google</a>

选项2-如果必须将动态查询字符串附加到重定向URL,则执行。

<a ng-click="redirectMe()">Google</a>

我的建议使用选项1参考-https://www.w3.org/TR/html5/links.html#attr-超链接目标

最新更新