Angular 2 Router, href links and unwanted page refresh

↘锁芯ラ 提交于 2019-12-04 03:09:20

问题


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

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


回答1:


You should use a routerLink instead. For example <a [routerLink]="[ '/MyCmp', {myParam: 'value' } ]">

See also https://angular.io/guide/ajs-quick-reference#ng-href

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




回答2:


You can try passing the json in the html template on routerLink like this.

@Component({
  selector: 'app',
  template: `
    <a [routerLink]="routerData">
  `
})
class AppComponent {
  this.routeData = [ '/MyCmp', {myParam: 'value' } ]
}

If you have array of link you can still try something like this with *ngFor



来源:https://stackoverflow.com/questions/34787262/angular-2-router-href-links-and-unwanted-page-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!