在Angular Route中传递int数组 [英] Pass array of int in Angular Route

查看:50
本文介绍了在Angular Route中传递int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Well, I did some research before asking this question.

这是我的路

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

当我单击下面的AppComponent中的购物车时,我想传递productIds.我可以这样做吗?

I want to pass productIds when click on cart like below in AppComponent. Can I do this ?

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

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

Am trying to access these productIds in ngOnit in cartcomponent like this

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

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

My question is, Is this the efficient way of passing and accessing array of Int between two components?

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

how do we maintain state(not sure whether this is right word, my background is ASP.NET) between components ?

推荐答案

使用 service 将ID数组从一个组件保存到另一组件.这不是通过URL传递的安全方法.

Use a service to save the array of ids from one component to other component. It's not a safe way to pass it over the url.

创建这样的服务

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

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

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

}

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

Then you can inject this Service into both the components and save it from the first component like,

this.appMsgService.saveIds(yourIds);

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

then retrieve in the second component as,

this.appMsgService.retrieveIDs(yourIds);

您可以按以下方式向您的组件中注入

you can inject to your component as,

constructor(
   private appMsgService: AppMessageQueuService
)

这篇关于在Angular Route中传递int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆