在不相关的Angular组件之间共享数据

时间:2020-06-25 12:37:14

标签: angular typescript angular8

如本文所述,我正在使用服务在两个不相关的控制器之间共享数据

https://www.infragistics.com/community/blogs/b/infragistics/posts/simplest-way-to-share-data-between-two-unrelated-components-in-angular

这是我用来共享数据的服务

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

@Injectable({
  providedIn: 'root'
})
export class AppService {
  tempfile = "temp";

  file: BehaviorSubject<string>;
  count: BehaviorSubject<number>;
  constructor() {

    this.file = new BehaviorSubject(this.tempfile);
  }

 
}

我将此服务注入了我的组件,但是当我尝试从服务中获取价值时

this.appsevice.file.subscribe(c => {
        this.file = c;
      });

我不断收到以下错误

this.appsevice.file.subscribe不是函数

0 个答案:

没有答案