1
我有問題,一些問題上angular2路由參數應用,則params是不確定的,我將向這個項目的一些代碼: 它route.moduleAngular2路由,路由參數
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);
}
}
有什麼辦法可以工作嗎?
'RouterModule.forChild([ {路徑: '學生/註冊',成分:StudentRegisterComponent} ])'套這個特定的路線,不要用以前的路由表 – Michael
謝謝,這可能是導致該錯誤的問題之一,我改變了這RouteModule.forRoot([{路徑:「學生/註冊」,成分:StudentRegisterComponent}]),但它仍然是t他同樣的情況 –