相关疑难解决方法(0)

如何使用共享服务将数据从一个组件发送到另一个组件

我想使用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");
}

}
Run Code Online (Sandbox Code Playgroud)

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  { …
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular-services angular

4
推荐指数
1
解决办法
3258
查看次数