在 Angular Route 中传递 int 数组

2023-12-31

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

这是我的路

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

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

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

我正在尝试像这样在 cartcomponent 的 ngOnit 中访问这些 ProductId

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

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

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


Use a 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
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Angular Route 中传递 int 数组 的相关文章