RouteParams in Angular 2 rc1

前端 未结 2 1082
名媛妹妹
名媛妹妹 2021-01-05 02:47

I\'ve been trying out Angular 2 since beta, and now with rc.0+ some things have changed.

One of those are RouteParams which cannot be imported from @angular/router.

相关标签:
2条回答
  • 2021-01-05 03:05

    One way is

      routerOnActivate(curr: RouteSegment) {
        this.userName = curr.getParam('userName');
        this.projId = curr.getParam('projId');
      }
    
    0 讨论(0)
  • 2021-01-05 03:07

    You have to use RouteSegment instead of using RouteParams in angular2 RC. like this :-

    import { Component } from '@angular/core';
    
    import { Routes, RouteSegment, ROUTER_DIRECTIVES } from '@angular/router';
    
    @Component({
    
      selector: 'about-item',
    
      template: `<h3>About Item Id: {{id}}</h3>`
    
    })
    
    class AboutItemComponent { 
    
      id: any;
    
      constructor(routeSegment: RouteSegment) {
    
        this.id = routeSegment.getParam('id');
    
      }
    }
    
    @Component({
    
        selector: 'app-about',
    
        template: `
    
          <h2>About</h2>
            <a [routerLink]="['/about/item', 1]">Item 1</a>
            <a [routerLink]="['/about/item', 2]">Item 2</a>
          <div class="inner-outlet">
            <router-outlet></router-outlet>
          </div>
        `,
        directives: [ROUTER_DIRECTIVES]
    })
    
    @Routes([
    
      { path: '/item/:id', component: AboutItemComponent }
    
    ])
    
    export class AboutComponent { }
    
    0 讨论(0)
提交回复
热议问题