Angular 2 Router,href链接和不需要的页面刷新 [英] Angular 2 Router, href links and unwanted page refresh

查看:150
本文介绍了Angular 2 Router,href链接和不需要的页面刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试验angular2(beta1),对新路由器感到有些困惑.

i am currently experimenting with angular2 (beta1) and i'm a bit puzzled about the new router.

通过router.navigate进行导航就像一个超级按钮,同时尝试通过到已注册路线的普通链接进行导航时,会刷新页面. 当然,PathLocationStrategy会发生这种情况,因为HashLocationStrategy会按预期工作.

Navigating via router.navigate works like a charm, while trying to do that via a normal link to a registered route refreshes the page. This happens with PathLocationStrategy of course, as HashLocationStrategy works as expected.

这是错误还是正常行为?

Is this a bug or the normal behaviour?

TIA

推荐答案

您应该改用routerLink.例如<a [routerLink]="[ '/MyCmp', {myParam: 'value' } ]">

另请参见 https://angular.io/guide/ajs-quick -reference#ng-href

您可以将事件侦听器添加到<a href=...元素或父级,然后调用router.navigate...event.preventDefault().

You could add event listeners to the <a href=... elements or a parent and call router.navigate... and event.preventDefault().

这篇关于Angular 2 Router,href链接和不需要的页面刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆