【问题标题】:Angular - Service data not passed between componentAngular - 组件之间未传递服务数据
【发布时间】:2020-07-23 22:45:00
【问题描述】:

我完全绝望地来找你......从 1 天开始,我尝试使用服务在我的两个组件之间传递数据。

我已经在另一个应用程序中使用了此服务,并且我知道它可以正常工作。

但是我已经尝试了使用谷歌搜索找到的所有东西:BehaviorSubject、Subject ... 但在我的情况下没有任何效果:由于第一个组件,服务中的值发生了变化,但在第二个组件,即使我订阅了服务中的价值。

第二个组件在初始化时获得值,但在...之后什么都没有

提示:第二个组件是第一个组件的父组件。

这是我的代码(实际上):

GlobalFn.Service :

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

@Injectable({
  providedIn: 'root',
})
export class globalFnService{
    id:any;
    activeDeepView = new Subject<boolean>();
    constructor() { 
      this.activeDeepView.next(false);
     }

    deepView(){
      this.activeDeepView.next(!this.activeDeepView);
    }

    getValue(): Observable<any> {
        return this.activeDeepView.asObservable();
    }

}

第一个组件(子):

import { Component, OnInit } from '@angular/core';
import { globalFnService } from '../services/globalFn.service';

@Component({
  selector: 'app-profil',
  templateUrl: './profil.component.html',
  styleUrls: ['./profil.component.scss'],
  providers: [globalFnService]
})
export class ProfilComponent implements OnInit {

  constructor(
    private globalFn:globalFnService
  ) { 

   }

  deepView(){
    this.globalFn.deepView();
    console.log("clicked !");
  }

  ngOnInit() {
    this.globalFn.getValue().subscribe(value => {
      if(value == true){
        console.log("status",value);
      } else {
        console.log("status",value);
      }
    })
  }

}

第二个组件(父组件):

import { Component, OnInit } from '@angular/core';
import { globalFnService } from '../services/globalFn.service';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss']
})
export class HomeComponent implements OnInit {



  constructor(
    private globalFn:globalFnService
  ) { 
    this.subscription = this.globalFn.getValue().subscribe(value => {
      if(value == true){
        console.log("status",value);
      } else {
        console.log("status",value);
      }
    }) 
   }

}

如果你能找到我做错的地方,你就有能力拯救我的一天......

谢谢!

【问题讨论】:

  • 您到底希望发生什么?
  • 必须在模块providers: [globalFnService]提供服务
  • 显示 app.module.ts
  • 我忘记在我的 app.module.ts 中添加它...我会尝试添加它,我来找你!
  • @PositivProd 不要将其添加到您的应用模块中。

标签: angular rxjs


【解决方案1】:

在组件之间共享数据时,您所要做的就是创建一个服务并将其注册到父模块(它将作为一个单例实例工作),以便可以跨所有组件使用:

需要应用的更改:

app.module.ts:

import { globalFnService } from '../services/globalFn.service'; // imports

providers: [
   ...,
   globalFnService
]

不需要添加对任何其他地方的引用:(在其他组件的提供者的数组中)。

【讨论】:

  • 这里也不需要将服务添加到提供者,因为服务本身是用`providedIn: 'root'`装饰的
  • @EvanOgra 是的,Check it
  • 谢谢,但即使我已经提供了In:'root',在我将它导入到providers 的app.module.ts 中之前它也不起作用...
  • @EvanOgra 在某一时刻,我也感到震惊! Unable to reproduce。我错过了什么吗?
  • @PrashantPimpale 你在你的 stackblitz 中安装了 Angular v5,它不支持提供的语法
【解决方案2】:

当您输入 providers: [globalFnService] 时,您是在告诉 Angular 实例化一个新的 globalFnService 并将其注入到组件中,而不是注入现有的服务实例。从组件中删除它,它应该可以工作。

【讨论】:

  • 不要将它从它也应该存在的组件中删除
  • @HitechHitesh 不,不应该。该服务被装饰为providedIn: 'root'。不应将其添加到任何 providers(除非您想要该服务的不同实例)。
猜你喜欢
  • 2019-03-20
  • 2018-12-31
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 2018-10-03
  • 2020-01-27
相关资源
最近更新 更多