【问题标题】:How to send data from one component to another using a shared service [duplicate]如何使用共享服务将数据从一个组件发送到另一个组件[重复]
【发布时间】:2018-02-09 18:59:09
【问题描述】:

我想使用 subject 将数据发送到另一个组件(出于盈利目的)。我无法取回数据。这是我的代码:

app.component.ts

import { Component } from '@angular/core';
import { shareService } from './share.service';

@Component({
 selector: 'my-app',
  template: `
  <hello></hello>
  <button (click)="passData()">
    Start
  </button>
  `,
  styleUrls: [ './app.component.css' ],
  providers:[shareService]
})
export class AppComponent  {
  constructor(private service : shareService){}

  passData(){
   this.service.send("hello");
}

}

hello.component.ts

import { Component, Input } from '@angular/core';
import { shareService } from './share.service';
import { Subscription }   from 'rxjs/Subscription';

@Component({
  selector: 'hello',
  template: `<h1>Hello!</h1>`,
  styles: [`h1 { font-family: Lato; }`],
  providers:[shareService]
})
export class HelloComponent  {
  subscription: Subscription;
    constructor(private share : shareService){
    this.subscription =  share.subj$.subscribe(val=>{
    console.log(val);
    })
  }
}

share.service.ts

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

@Injectable()
export class shareService{

  private sub = new Subject();
  subj$ = this.sub.asObservable();

    send(value: string) {
    this.sub.next(value);
  }

}

我没有在控制台中获得价值。

这里是工作演示:DEMO

【问题讨论】:

标签: javascript angular typescript angular-services


【解决方案1】:

通过放置:

@Component({
  .....
  providers: [sharedService]
})

在这两个组件中,您正在创建共享服务的两个不同实例。 每个实例都不“知道”来自每个组件的数据。 在模块级别提供它并创建一个单例服务:

@NgModule({
  ....
  providers: [sharedService]
})

这样,您将服务作为单个实例注入到两个组件中,因此它们可以像共享数据一样共享它。

或者使用Angular's preferred new way

从 Angular 6.0 开始,创建单例的首选方式 service 是在服务上指定它应该在 应用程序根。这是通过将providedIn设置为root来完成的 服务的@Injectable 装饰器:

@Injectable({
  providedIn: 'root',
})

Demo

also

【讨论】:

  • 听起来不错
  • 但是如果我在每个组件中分别提供它不应该工作吗?
  • 谢谢!它奏效了。
  • 好的!!谢谢@Vega
  • 是的,完成了!!!
【解决方案2】:

我不知道为什么要使用 sub$,但你不需要它

// just push data to subject. you can use BehavourSubject to initiatte a value.
@Injectable()
export class shareService{

  private sub = new Subject();

    confirmMission(astronaut: string) {
    this.sub.next(astronaut);
  }

}

然后在你的第二个组件中订阅它

@Component({
  selector: 'hello',
  template: `<h1>Hello!</h1>`,
  styles: [`h1 { font-family: Lato; }`],
  providers:[shareService]  // this can be shared in module lebel or componenet level
})
export class HelloComponent  {
  subscription: Subscription;
    constructor(private share : shareService){
    this.subscription =  share.subj.subscribe(val=>{
    console.log(val);
    })
  }
} 

确保在模块级别提供您的服务或在两个组件中提供。

【讨论】:

  • stackoverflow.com/questions/36986548/… 是仅通过可观察接口公开内部主题的好习惯
  • 所以使用 asObservable 是不是很好?
  • 我从未使用过。我解释的方式我一直在使用它。对我来说,它的额外代码行需要编写和维护
  • @Sampath1504 当你的组件(客户端)只关心它的可观察行为时,它关于隐藏共享服务的内部实现和主体的观察者行为
  • @Jota.Toledo 和 Aniruddha Das 。你能帮我解决这个问题吗?这是我的演示 stackblitz.com/edit/add-remove-components 。我在每个元素上都有一个关闭按钮,如果关闭它,我的组件将被删除,但为什么我无法再次添加相同的组件?EG:如果我关闭组件 1 并尝试再次添加它,它不起作用跨度>
猜你喜欢
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 2021-04-23
  • 2021-05-02
  • 2019-03-18
  • 2016-04-20
  • 2020-12-06
  • 1970-01-01
相关资源
最近更新 更多