首页 > 解决方案 > 在路由中传递自定义类型作为参数 -> 返回:[object Object]

问题描述

单击链接时,我需要将自定义类型的变量传递给非子/父组件。

自定义类型

export class Dati {
  fileJson;
  ricette: string[][];
  n_Ricette: 0;
  nPostazioni: number;
  postazioniNome: string[];
  defPostazione = "FRIGGITRICI";
}

及其在app.component.ts中的初始化:

dati : Dati = {
    fileJson: "",
    ricette: [[],[]],
    n_Ricette: 0,
    nPostazioni: 0,
    postazioniNome: [],
    defPostazione: "FRIGGITRICI",
  }

应用程序路由模块

....
const routes: Routes = [
  { path: 'option.component/:dati', component: OptionComponent },
]
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
....

带有路由的 html 链接(app.component.html)

  <a routerLink="/option.component/{{dati}}" >Opzioni</a>

option.component.ts(路由的目的地)

  constructor(private route: ActivatedRoute) { 
    this.route.params
      .subscribe(params => {
        console.log(<Dati>(params["dati"]))
      });
  }

中的结果console.log()是:[object Object]

如何获取我的数据?

附言。链接工作正常,发送的数据是正确的

标签: angulartypescriptobjectparametersangular-router

解决方案


推荐阅读