【发布时间】:2017-09-30 12:04:22
【问题描述】:
您好,我想通过 Angular 4 路由传递一些参数
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { StartGameComponent } from './start-game/start-game.component';
import { GameComponent } from './game/game.component';
const appRoutes: Routes = [
{ path: '', redirectTo: '/first', pathMatch: 'full' },
{ path: 'startGame', component: StartGameComponent },
{path: 'game/:width/:height',component: GameComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(appRoutes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}
在组件中StartGameComponent
goToGameComponent(width:string,height:string){
this.router.navigate(['game', {width:width,height:height}]);
}
在组件中GameComponent
ngOnInit() {
this.route.params.forEach((urlParams) => {
this.width= urlParams['width'];
this.height=urlParams['height'];
});
在 app.component.html 中
<div>
<md-toolbar color="primary">
<span>MineSweeper Wix</span>
</md-toolbar>
<router-outlet></router-outlet>
<span class="done">
<button md-fab>
<md-icon>check circle</md-icon>
</button>
</span>
</div>
这引发了我的错误
无法匹配任何路线。 URL 段:'game;width=10;height=10'
【问题讨论】: