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

查看:36
本文介绍了Angular 2 路由器、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.例如

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

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

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

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

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