Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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

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 组件4之间的数据传递_Angular_Typescript_Web - Fatal编程技术网

Angular 组件4之间的数据传递

Angular 组件4之间的数据传递,angular,typescript,web,Angular,Typescript,Web,我学习Angular 4已经有几个星期了。我似乎不知道如何将数据从一个组件传递到另一个组件,因为我的组件以前很简单。我发现最适合我使用的是Behaviorsubject,因为我有一个表单组件,当我编辑客户而不是创建一个新客户时,它会从列表组件中填充 客户列表 import { Component, OnInit } from '@angular/core'; import { DataService } from '../services/data.service'; import {Subsc

我学习Angular 4已经有几个星期了。我似乎不知道如何将数据从一个组件传递到另一个组件,因为我的组件以前很简单。我发现最适合我使用的是Behaviorsubject,因为我有一个表单组件,当我编辑客户而不是创建一个新客户时,它会从列表组件中填充

客户列表

import { Component, OnInit } from '@angular/core';
import { DataService } from '../services/data.service';
import {Subscription} from 'rxjs/Subscription';

@Component({
  selector: 'app-cust-list',
  templateUrl: './cust-list.component.html',
  styleUrls: ['./cust-list.component.css']
})
export class CustListComponent implements OnInit {

  isEdit:boolean = false;
  subscription:Subscription;

  customers: any[];
  customer = {
    id: '',
    name: '',
    email: '',
    phone: ''
  }

  constructor(public dataservice:DataService) { 
    console.log('werkz');
  this.dataservice.get().subscribe (customers =>{
    this.customers = customers;
  });  

  if(this.isEdit){
    this.subscription = this.dataservice.setCust.subscribe(customer => 
 this.customer = customer)
    }
  }
客户绩效

import { Component, OnInit } from '@angular/core';
import { DataService } from '../services/data.service';
import { Subscription } from 'rxjs/Subscription';

@Component({
  selector: 'app-cust-form',
  templateUrl: './cust-form.component.html',
  styleUrls: ['./cust-form.component.css']
})
export class CustFormComponent implements OnInit {

  subscription:Subscription;

 constructor(public dataservice: DataService){
  this.subscription = this.dataservice.getCust(cust);
 }}
数据服务

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';

@Injectable()
export class DataService{
public custSent = new BehaviorSubject<object>([]);

    set setCust(customer){
        this.custSent.next(customer);
}

get getCust(){
    return this.custSent.getValue();
}
}
从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Http};
导入'rxjs/add/operator/map';
从“rxjs/Observable”导入{Observable};
从'rxjs/BehaviorSubject'导入{BehaviorSubject};
@可注射()
导出类数据服务{
public custssent=新行为主体([]);
set setCust(客户){
this.custssent.next(客户);
}
获取getCust(){
返回此.custssent.getValue();
}
}

这是正确的方法吗?如果是这样,有人能告诉我出了什么问题吗?如果不是,我可能需要朝正确的方向推动。提前感谢。

更改服务功能名称如下(删除
get
set
):

从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Http};
导入'rxjs/add/operator/map';
从“rxjs/Observable”导入{Observable};
从'rxjs/BehaviorSubject'导入{BehaviorSubject};
@可注射()
导出类数据服务{
public custssent=新行为主体([]);
setCust(客户){
this.custssent.next(客户);
}
getCust(){
返回此.custssent.getValue();
}
}

您测试过吗?它有用吗?如果没有,你能举例说明具体的问题吗?我给出的这个答案如果没有测试,就不要发布你的问题
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';

@Injectable()
export class DataService{
public custSent = new BehaviorSubject<object>([]);

setCust(customer){
        this.custSent.next(customer);
}

getCust(){
    return this.custSent.getValue();
}
}