Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何将多个值传递给角度5可观测值?_Angular_Typescript_Angular5 - Fatal编程技术网

Angular 如何将多个值传递给角度5可观测值?

Angular 如何将多个值传递给角度5可观测值?,angular,typescript,angular5,Angular,Typescript,Angular5,我正在一天一天地学习,但我被一个问题困住了。一个组件中有3个值,我需要将它们传递给另一个组件。我对observable和subscription有基本的了解,但我使用了1个参数。现在我想广播3个值 这是我的购物车代码 additem(itemText){ this.cart.push(itemText); this.itemscount = this.cart.length; this._data.changeCart(this.cart); } 当我点击一个产品时,这个函数调用它

我正在一天一天地学习,但我被一个问题困住了。一个组件中有3个值,我需要将它们传递给另一个组件。我对observable和subscription有基本的了解,但我使用了1个参数。现在我想广播3个值

这是我的购物车代码

additem(itemText){
  this.cart.push(itemText);
  this.itemscount = this.cart.length;
  this._data.changeCart(this.cart);
}
当我点击一个产品时,这个函数调用它,它将cart数组发送给observable,在那里我广播cart数组

export class DataService {

    private cart = new BehaviorSubject<any>([]);
    cast = this.cart.asObservable();

    constructor() { }

    changeCart(item_param) {
        this.cart.next(item_param);
        //console.log(this.cart);
    }

}
导出类数据服务{
私有购物车=新行为主体([]);
cast=this.cart.asObservable();
构造函数(){}
changeCart(项目参数){
this.cart.next(项目参数);
//console.log(this.cart);
}
}
现在我需要将3个值传递给可观察对象,我不知道该怎么做,因为我读到我们只能将一个值传递给可观察对象。 我有一个想法,将所有值包装在一个数组中并传递该数组,但我在这个数组中搜索,但找不到如何将多个值存储在单个数组中


任何帮助都将不胜感激。谢谢这里介绍如何将其作为对象并用于多个值

export class DataService {

    private cartObject = new BehaviorSubject<Object>({});
    cartObjectInfo = this.cartObject.asObservable();

    constructor() { }

    changeCart(cartObject) {
        this.cartObject.next(cartObject);
    }
}

您可以这样制作一个对象:

a = 5;
b = 'sss';
c = new Date();

objToPass = { a: a, b: b, c: c };
稍后,您可以访问对象属性,如objToPass.a、objToPass.b等


这是构造JS对象的标准方法。

将其设为对象,然后将其用于多个值谢谢回答。我明白你的意思,但是我是否应该把它写在附加项方法this中。_data.changeCart(this.cart,b,c)??我实际上是在问如何通过一个物体。您的答案是在dataservice中创建对象。我想从组件类传递它。有什么办法吗?然后只在组件中创建对象,只传递一个参数,这正是我要问的。但我不知道如何用angular2制作物体?你能提供一个示例代码吗?谢谢你的帮助。我的问题已经解决了,但是我在代码组织方面有一个小问题。我正在发送这个对象this.cartobj={cart\u items:this.cart,items\u count:this.itemscont}我正在访问组件html文件中的对象,如下所示*ngFor=“let items of cartobj.cart_items”和{{cartobj.items_count}}但我想将对象值分配给组件类文件中的数组和简单变量,然后在html文件中访问。我试过像这样的ngOnInit(){this.cart=this.cartobj.cart_items;}但它不起作用,我没有得到任何值。有没有办法访问组件类中的对象值,然后将它们发送到html模板?
a = 5;
b = 'sss';
c = new Date();

objToPass = { a: a, b: b, c: c };