代码之家  ›  专栏  ›  技术社区  ›  techno

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

  •  0
  • techno  · 技术社区  · 4 年前

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

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

    0 回复  |  直到 4 年前