简体   繁体   English

共享服务无法将数据传递到下一个组件

[英]shared service is unable to pass data to next component

I have created two components and one shared service, i want pass data from one component to another, but i am getting empty object bellow is 1st component 我创建了两个组件和一个共享服务,我想将数据从一个组件传递到另一个组件,但是我收到的空对象是第一个组件

import { Component, OnInit } from '@angular/core';
import {SharedService} from './../shared.service';
import { Router, NavigationStart } from '@angular/router';



    @Component({
      selector: 'app-cone',
      templateUrl: './cone.component.html',
      styleUrls: ['./cone.component.css'],
      providers: [SharedService]
    })
    export class ConeComponent implements OnInit {
    req = <any>{};
      constructor(public service:SharedService,private router:Router) { }
       send(){
        this.req.fname= "ketan";
        this.req.lname= "pradhan";
        this.service.saveData(this.req); 
        console.log('str');
        this.router.navigate(['/apps/ctwo']);
      }

      ngOnInit() {

      }

    }

Bellow is the 2nd component where i need to pass the data from 1st comonent, i am getting empty object is this.myName 波纹管是第二个组件,我需要从第一个组件传递数据,我得到的是空对象。

import { Component, OnInit } from '@angular/core';
import {SharedService} from './../shared.service';
import { Router, NavigationStart } from '@angular/router';

@Component({
  selector: 'app-ctwo',
  templateUrl: './ctwo.component.html',
  styleUrls: ['./ctwo.component.css'],
  providers: [SharedService]
})
export class CtwoComponent implements OnInit {
myName= <any>{};

  constructor(public service:SharedService,private router:Router) {
    this.myName=this.service.getData();
        console.log(this.myName);
   }

  ngOnInit() {

  }

}

Bellow is shared service which is for communicating between 2 components 贝娄是共享服务,用于2个组件之间的通信

import {Component, Injectable,Input,Output,EventEmitter} from '@angular/core' 从'@ angular / core'导入{Component,Injectable,Input,Output,EventEmitter}

// Name Service export interface myData { name:string, lname:string } //名称服务导出接口myData {name:string,lname:string}

@Injectable()
export class SharedService {
  sharingData: myData=<any>{};
  saveData(str){
    console.log('save data function called' + str.fname);
    console.log(this.sharingData);
    this.sharingData = str; 
    // this.sharingData.lname=str.lname; 
    console.log(this.sharingData)
  }
  getData()
  {
    console.log('get data function called');
    return this.sharingData;
  }
} 

When you are setting providers arrays at component level, it means that you have two separate instances of the service in this case. 在组件级别设置providers数组时,这意味着在这种情况下,您有两个单独的服务实例。

You need to declare the service in your NgModule providers array instead, then the two components (and any other components in that module) will have the same instance of the service. 您需要改为在NgModule providers数组中声明该服务,然后这两个组件(以及该模块中的任何其他组件)将具有相同的服务实例。

So remove the providers arrays in your components, and instead add the service to providers array in your NgModule . 因此,请删除组件中的providers数组,然后将服务添加到NgModule providers数组。

@Component({
  selector: 'app-ctwo',
  templateUrl: './ctwo.component.html',
  styleUrls: ['./ctwo.component.css'],
  // providers: [SharedService] // remove these!
})

and instead.... 而是...

@NgModule({
  imports: [ ... ],
  declarations: [ .. ],
  bootstrap: [ ... ],
  providers: [ SharedService ] // here!
})

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

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