在 Angular 路由中传递 int 数组

Pass array of int in Angular Route

好吧,我在问这个问题之前做了一些研究。

这是我的路

{ path: 'cart/:productIds', component: CartComponent }

我想在 AppComponent 中点击购物车时传递 productIds,如下所示。我可以这样做吗?

 goToCart() {
this.router.navigate(['/cart', this.productIds]);}

我正在尝试像这样在购物车组件的 ngOnit 中访问这些产品 ID

this.productIds = route.snapshot.params["productIds"];

我的问题是,这是在两个组件之间传递和访问 Int 数组的有效方式吗?

我们如何维护组件之间的状态(不确定这是否正确,我的背景是 ASP.NET)?

使用 service 将 id 数组从一个组件保存到另一个组件。通过 url.

传递它不是一种安全的方法

创建这样的服务,

import { Injectable } from '@angular/core';
@Injectable()
export class AppMessageQueuService {

    myIds: Array<Number>=[];
    constructor() {

    }
    saveIds(produIds:any){
        this.myIds = produIds;
    }
    retrieveIDs(){
        return this.myIds;
    }

}

然后您可以将此服务注入到两个组件中,并从第一个组件中保存它,例如,

this.appMsgService.saveIds(yourIds);

然后在第二个组件中检索为,

this.appMsgService.retrieveIDs(yourIds);

你可以注入你的组件,

constructor(
   private appMsgService: AppMessageQueuService
)