如何将对象从一个组件传递到另一组件

时间:2018-09-17 06:16:35

标签: angular6

我对angular 6还是陌生的。我正在尝试将对象详细信息从一个组件发送到另一个组件。

我尝试成功传递字符串值。但是,当我尝试发送对象时,我遇到了问题。谁能指导我我在哪里做错了。

下面是我尝试过的代码。 已创建 data-share.service.ts 文件。

    import { Injectable } from '@angular/core';
import { BehaviorSubject} from 'rxjs/BehaviorSubject'

@Injectable({
  providedIn: 'root'
})
export class DataShareService {

  private messageSource = new BehaviorSubject<string>('default message');
  currentMessage = this.messageSource.asObservable();


  private mastProjClickedValDetail = new BehaviorSubject<any>([]);
  mastProjVal = this.mastProjClickedValDetail.asObservable();


  constructor() { }

  changeMessage(message: string){
    this.messageSource.next(message)
  }

  getMastProjDetail(MastProjObj: any){
    this.mastProjVal.next(MastProjObj)
  }
}

Project-Management.component.ts文件。

import { Component, OnInit,Output,EventEmitter } from '@angular/core';
import $ from 'jQuery';
import { OVERLAY_PROVIDERS } from '@angular/cdk/overlay';
import { ProjectManagementService } from '../services/project-management.service';
import { DataShareService } from '../services/data-share.service'

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

  records = {}

  public MastProjObj : any[];

  iconActive:Boolean;
  i:any;
  pageno:number;
  filter='';
  showSpinner: boolean = false;



  public data: any[];
  public total: number;
  public tenantIdVal: any[];


  ngOnInit() {  

    const userValue = JSON.parse(localStorage.getItem('loginDetails'));

    this.getProjectManagementListData(userValue,this.tenantIdVal);

 }

// ====================== To send Master project Details to other components. ======================

      mastProjGetDetailBtn(mastProjDetail){
        console.log("MastProjDetail---------------",mastProjDetail);
        this.MastProjObj = mastProjDetail;
        this.dataShare.getMastProjDetail(this.MastProjObj)
      }

// ====================== To send Master project Details to other components. ======================


 getProjectManagementListData(userValue,tenantIdVal): void {

  this.showSpinner = true;
  let data: Array<any>;

  this.records = this.ProjManagList.getProjectManagementListData(userValue,tenantIdVal).subscribe((data: any) => {

    console.log("data--",data);
  this.data = data.selectResults;
  this.total  = this.data.length;
  this.total=Math.ceil(this.total/10);
  },
  error => { 

    this.showSpinner = false;

  },
  () => {
    this.showSpinner = false; 
  })

}



      ngOnDestroy(){ // To remove active icon in sidebar.
        this.iconActive=false;
      }  

    }

data-share.service.ts 文件中的“可观察的” 类型的问题上,获取“下一个”属性不存在

0 个答案:

没有答案