0
我想路由到一個子模塊,路由參數,可以我app.routes看起來像angular2路由器將路由解釋爲參數?
import { Routes, RouterModule } from '@angular/router';
const appRoutes: Routes = [
{ path: 'cell', loadChildren: './cell/cell.module#CellModule', pathMatch: 'prefix'}
];
export const appRoutingProviders: any[] = [];
export const routing = RouterModule.forRoot(appRoutes);
我的孩子要素(小區)有以下途徑:
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CellComponent } from './cell.component';
const cellRoutes: Routes = [
{ path: ':id', component: CellComponent, pathMatch: 'full' }
];
export const cellRouting: ModuleWithProviders = RouterModule.forChild(cellRoutes);
和CellComponent
看起來是這樣的:
export class CellComponent implements OnInit {
@Input()
dep: string;
constructor(
private route: ActivatedRoute) { }
ngOnInit() {
this.route.params.forEach((params: Params) => {
this.dep = params['id'];
console.log(this.dep);
});
}
}
我希望能夠調用/cell/2
路線,以米獲得2
Ÿ控制檯,但是,如果我打這通電話,我得到一個錯誤:
TypeError: Cannot read property 'length' of undefined
這似乎是因爲使用了不確定的路線,如果我只是叫/cell
然後我的組件加載罰款,並打印cell
到安慰。爲什麼路由器將路由解釋爲參數?下面的代碼
都能跟得上 - 這並不能幫助我很害怕。我相信這是一個路由問題,因爲該組件在另一個項目中正常工作(我正在將此功能重構爲導致此問題的新模塊)。 –