我对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 );
}
}
有什么方法可以起作用吗?
答案 0 :(得分:3)
您没有使用指定Routes
的{{1}}对象:
试试这个:
token