简体   繁体   English

无法在angular2中显示从一个组件发送到另一个组件的数组

[英]cannot display array sent from one component to other in angular2

SERVICE-- 服务 -

import {Injectable} from '@angular/core';
import {UserData} from '../user-data/user-data.component';

@Injectable()
export class UserDataService {

    constructor(){}
    userdata:UserData[]; 

    getData(){
        console.log('service',this.userdata);
        return this.userdata;
    }

    setData(user:any){
        this.userdata=user;
        console.log(this.userdata);
    }
}

USER-DATA-class --- 用户数据类-

export class UserData {
    firstname: string;
    middlename: string;
    lastname: string;
    email: string;
    contact: number;
}

Component1 -- 组件1-

import { Component,OnInit,OnDestroy } from '@angular/core';
import { UserData } from '../../user-data/user-data.component';
import { ViewEditUser } from '../../view-edit-user/view-edit-user.component';
import {UserDataService} from '../../user-data-service/user-data-service.service';

@Component({
    selector: 'form-page',
    templateUrl: `app/add-user-sidebar/user-form/form.component.html`,
    providers:[UserDataService]
})

export class Form implements OnInit,OnDestroy {

    userdetail:UserData;
    constructor(private service:UserDataService){
    }

    addUser(first:string,middle:string,last:string,emailid:string,contactno:number){
        this.userdetail=({firstname:first,middlename:middle,lastname:last,email:emailid,contact:contactno})
        console.log(this.userdetail);
        this.service.setData(this.userdetail);  
    }

    ngOnInit(){
    }

    ngOnDestroy(){
    }
}

Component2-- Component2--

import { Component,Input, OnInit } from '@angular/core';
import { Form } from '../add-user-sidebar/user-form/form.component';
import {UserData} from '../user-data/user-data.component';
import { WelcomePage } from '../welcome-page/welcome-page.component';
import {UserDataService} from '../user-data-service/user-data-service.service';

@Component({
    selector:'view-edit',
    templateUrl: 'app/view-edit-user/view-edit-user.component.html',
    providers: [UserDataService]
})  

export class ViewEditUser implements OnInit {
  arraydata:any;
    constructor(private service:UserDataService){}

  // arraydata:any;
   printarray(){
       console.log(this.arraydata);
   }
    ngOnInit()
    {
        this.arraydata=this.service.getData();
        console.log("hhghdfghdf",this.arraydata);    
    }

}

I am new to angular2, I have two components in my module, one component is a form where user inputs data, that data is then sent to a service, when I console.log it then I can see the data in service. 我是angular2的新手,我的模块中有两个组件,一个组件是用户输入数据的一种形式,然后将数据发送到服务,当我进行console.log记录时,便可以看到服务中的数据。 but when I try to access that array from the second component then I can't access the data what to do? 但是当我尝试从第二个组件访问该数组时,我无法访问该怎么办?

If you provide the service on each component, you can't use it for communication, because each component will get a different service instance. 如果在每个组件上提供服务,则不能将其用于通信,因为每个组件将获得不同的服务实例。

If one component is a parent (ancestor) of the other component, only provide it on the parent component. 如果一个组件是另一组件的父级(祖先),则在父级组件上提供它。 Otherwise provide it on a component that is a parent (anjestor) of both or provide it only in @NgModule() to make the service global. 否则,在作为两者的父级(anjestor)的组件上提供它,或者仅在@NgModule()提供它以使服务成为全局。

You also need to be aware that it's possible that one component reads, before the other set the value, depending on where you set the value and in what order the components are created. 您还需要注意,一个组件可能会在另一个组件设置值之前读取,具体取决于您在何处设置该值以及创建组件的顺序。 Using a BehaviorSubject usually avoids this pitfall, because this way it doesn't matter which component is created first or if one component tries to read, while the other hasn't set the value yet. 使用BehaviorSubject通常可以避免这种陷阱,因为通过这种方式,首先创建哪个组件或一个组件尝试读取而另一个组件尚未设置值并不重要。

For shareing between to Angular instances see also How to share service between two modules - @NgModule in angular2? 有关在Angular实例之间共享的信息,另请参阅如何在两个模块之间共享服务-angular2中的@NgModule?

You nee to use observables to pass data between components. 您需要使用可观察对象在组件之间传递数据。

In your service create a Subject type variable and in the your first component do a .next to pass data to the service and in your 2nd component, subscribe to the service veriable and it will get you the data. 在您的服务中,创建一个Subject类型变量,然后在您的第一个组件中执行.next,以将数据传递给该服务;在您的第二个组件中,订阅服务veriable,它将为您获取数据。

You are not getting the data because of the async behavior of JavaScript which will be handled by observables 由于JavaScript的异步行为(将由可观察对象处理),因此您无法获取数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM