【问题标题】:passing multiple params in route angular 4在路线角4中传递多个参数
【发布时间】: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'

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    我需要一些东西,比如传递动态构造它们的多个查询参数。因此,请执行以下步骤来实现相同的目的:

    例如,

    const arrayItem = [{ source: "external" }, { fileFiler: "File Name 1" }];
    const queryParams = arrayItem.reduce((arrObj, item) => Object.assign(arrObj, item, {}));  // this will convert into single object. 
    
    // Output: {source: "external", fileFiler: "File Name 1"}

    最后调用下面的行将其传递给路由:

    this.router.navigate([], { relativeTo: this.activatedRoute, queryParams });
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      您将必需路由参数的语法与可选路由参数混合在一起。

      Angular 提供了三种类型的路由参数: 1) 必需参数。 2) 可选参数。 3) 查询参数。

      必填参数用于必填值,例如用于显示详细信息页面的 Id。您的情况需要宽度和高度吗?

      可选参数用于并非总是需要的值,例如将输入的搜索条件传递给应该使用该条件的列表页面。

      查询参数类似于可选参数,但您可以跨路由保留它们。所以如果你想路由到某个地方再返回,你可以保留参数。

      ONLY 必需的参数在路由配置中定义。可选参数和查询参数包含在路由配置中(因此路径只是“游戏”)

      设置参数的语法会因类型而异:

      必填参数:this.router.navigate(['game', width, height]);

      可选参数:this.router.navigate(['game', {width:width,height:height}]);

      查询参数:this.router.navigate(['game', { queryParams: {width:width, height:height}}])

      欲了解更多信息,请查看:https://app.pluralsight.com/library/courses/angular-routing/table-of-contents

      【讨论】:

      • 感谢您的精彩解释,它帮助我解决了我的问题。
      • 如何从另一边抓取参数?我的意思是从您导航到的组件中?
      • @DeborahK 我们如何传递一个字符串参数,其中包含空格,如“进程 1”?
      • 如果我的第二个参数是可选的怎么办?
      • @knigalye 您将必需参数作为必需参数处理,将可选参数作为可选参数处理。所以像这样:this.router.navigate(['game', game.id, {height: height}]);
      猜你喜欢
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多