简体   繁体   English

通过服务在指令/组件之间传递数据

[英]Passing Data between directive/component via service

I'm attempting to pass data from a directive to a component via a service. 我正在尝试通过服务将数据从指令传递到组件。 The return from my transfer service file says that the type 'Transfer' is not assignable to Observer . 我的传输服务文件返回的信息表明'Transfer'类型不可分配给Observer I also get ' Property 'subscribe' does not exist on type '() => Observable ' in my component file when trying to subscribe to the service. 当尝试订阅服务时,我的组件文件中的类型'()=> Observable ' 也不存在 ' 属性'subscribe' Why is this and how can I fix it? 为什么会这样,我该如何解决? my files are 我的文件是

transfer.service.ts 转移服务

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Transfer } from './transfer';

@Injectable()
export class TransferService {

  constructor(private transfer: Transfer){}

  private pageData;
  private contentData;
  private contentBox;

  setData(pData, cData, bData){
    this.pageData = pData;
    this.contentData = cData;
    this.contentBox = bData;
  }

  getData(): Observable<Transfer>{
    this.transfer.page = this.pageData;
    this.transfer.content = this.contentData;
    this.transfer.contentBox = this.contentBox;
    this.clearData();
    return this.transfer;
  }

  clearData(){
    this.pageData = undefined;
    this.contentData = undefined;
    this.contentBox = undefined;
  }
}

transfer.ts 转移

import { ElementRef } from '@angular/core';
export interface Transfer{
    page: [
        {
            pageNum: number,
            subStart: number,
            subEnd: number
        }   
    ];
    content: string;
    contentBox: ElementRef;
}

article.component.ts - component article.component.ts-组件

  transfer(){
    this.ts.getData.subscribe(data => this.tsData = data);
  }

paginator.ts - directive paginator.ts-指令

// Pass date via transferservice.ts
transfer(){
  this.ts.setData(this.pageJump, this.content, this.contentBox);
}

EDIT: using @ritaj's solution resolved the issue of not being assignable/property not existing, but now I'm getting ' Error: Can't resolve all parameters for TransferService ' 编辑:使用@ritaj的解决方案解决了无法分配/属性不存在的问题,但是现在我得到了“ 错误:无法为TransferService解析所有参数

STACKBLITZ EDIT: https://stackblitz.com/edit/mesh STACKBLITZ编辑: https ://stackblitz.com/edit/mesh

Your getData() service method does NOT return Observable, as you have type d. 您的getData()服务方法不会返回Observable,因为您type d。

It just returns a normal JS object, which does not have .subscribe() method, obviously. 显然,它仅返回一个普通的JS对象,该对象没有.subscribe()方法。

Change your article.component.ts transfer() method to this: 将您的article.component.ts transfer()方法更改为此:

transfer(){
    this.tsData = this.ts.getData();
  }

And change getData signature to: 并将getData签名更改为:

getData(): Transfer {}

article.component.ts article.component.ts

  constructor(public ts : TransferService){}
  transfer(){
     this.tsData = this.ts.getData();   // directly call
  }

Inject the service in app.module.ts :- 将服务注入app.module.ts:-

providers:[TransferService]

Here is Stackblitz link, just a demo to reflect solution to the problem:- 这是Stackblitz链接,仅是一个反映问题解决方案的演示:

https://stackblitz.com/edit/angular5-service-example-onwtg6?file=app/article.component.ts https://stackblitz.com/edit/angular5-service-example-onwtg6?file=app/article.component.ts

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

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