Angular 2 Router, href links and unwanted page refresh

后端 未结 2 730
慢半拍i
慢半拍i 2021-01-17 17:32

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

相关标签:
2条回答
  • 2021-01-17 17:55

    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

    0 讨论(0)
  • 2021-01-17 18:01

    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().

    0 讨论(0)
提交回复
热议问题