代码之家  ›  专栏  ›  技术社区  ›  alim1990

使用navigate()发送数组的角度8返回的是一个对象,而不是数组本身

  •  -2
  • alim1990  · 技术社区  · 5 年前

    我想在 .navigate() 角8路由模块。

    在home组件中,结果是:

    console.log(this.offers)
    

    正在返回所有可用的报价及其所有相关数据。

    在那之后,我派 this.offers 在导航中:

    this.router.navigate(['/offers'], {queryParams:{ 'offers': this.offers}});
    

    在offers组件中,我尝试使用以下方法获取报价数组:

    console.log(this.activatedRouter.snapshot.queryParams.get('offers'))
    

    但结果是 [Object Object] .

    我试着用 this.activatedRouter.snapshot.params['offers'] 但它返回了同样的东西。

    我尝试了以下方法:

    console.log(this.activatedRouter.snapshot.queryParamMap.get('offers'))
    

    但它返回了一个[Object Object]:

    enter image description here

    0 回复  |  直到 5 年前
        1
  •  1
  •   Ashish Patel    5 年前

    而不是使用 activatedRouter.snapshot ,订阅 this.activatedRoute.queryParams 主动发出查询参数中任何更改的流。

    @Component({ ... })
    export class MyComponent implements OnInit {
      order: string;
      constructor(private route: ActivatedRoute) { }
    
      ngOnInit() {
        this.route.queryParams
          .filter(params => params.offers)
          .subscribe(params => {
            console.log('Query Params: ', params); // {offers: <value>}
          });
      }
    }
    
        2
  •  0
  •   alim1990    5 年前

    有几种解决方法 this link 中号的。

    我要用的是 this.navigate .

    所以解决方案是在原始组件上:

    this.router.navigate(['/offers'], { state: {'offers': this.offers, 'info': this.info}});
    

    在目的地组件(在我的情况下,它不是子组件)上,我们应该使用 state 从angular 7.2+新增的附加功能:

    this.offers = window.history.state.offers;
    

    我们不能使用 snapshot queryParams 在这种情况下,根据上面的链接,它将在 ngOnInit() 被处决。所以最好的解决方案是使用 状态 额外的

    推荐文章