Angular2路由,路由参数

时间:2017-06-29 12:18:08

标签: angular url-routing token

我对angular2上的路由参数问题有疑问,params未定义,我将展示该项目的一些代码: 它的route.module

    import { NgModule } from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {StudentRegisterComponent} from './student-register/student-register.component';

const routes: Routes = [

    { path: 'student/register', component: StudentRegisterComponent },
    { path: 'student/register/:token', component: StudentRegisterComponent }
];

@NgModule({
    imports: [
        RouterModule.forChild([
            { path: 'student/register', component: StudentRegisterComponent }
        ])
    ],
    exports: [RouterModule]
})
export class StudentRoutingModule { }

和组件文件:

import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import {Router, ActivatedRoute, Params} from '@angular/router';
@Component({
    moduleId: module.id.toString(),
    selector: 'student-register',
    templateUrl: 'student-register.component.html'
})
export class StudentRegisterComponent {
constructor(private activatedRoute: ActivatedRoute
    )
ngOnInit() {
    // subscribe to router event
        let param1 = this.activatedRoute.snapshot.params['token'];
        console.log('params: ' + param1 );
}
}

有什么方法可以起作用吗?

1 个答案:

答案 0 :(得分:3)

您没有使用指定Routes的{​​{1}}对象:

试试这个:

token