首页 > 解决方案 > 在不相关的 Angular 组件之间共享数据

问题描述

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

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.appservice.file.subscribe 不是函数

标签: angulartypescriptangular8

解决方案


推荐阅读