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

查看:19
本文介绍了在 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]);}

我正在尝试像这样在购物车组件中的 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天全站免登陆