将价值传递给主题服务Angular JS

发布于 2025-02-10 04:29:22 字数 1501 浏览 1 评论 0原文

我尝试使用主题服务对儿童组件进行Passa值,我基于 Docuctatio ,但是当尝试打印数据时,可以没有任何显示。

我有NAVCOMPONENT在SELECT输入上请求API基础。更改选择更新请求时,并将新数据发送到儿童页面。

服务:



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

@Injectable()
export class SearchService {
    // Observable string sources
    private data = new Subject<any>();

    // Observable string streams
    value$ = this.data.asObservable();

    // Service message commands
    setValeu(value: any) {
        this.data.next(value);
    }


}

NAVCOMPONENT:

 async consultaIndicadores() {
    try {
      this.loading = true
      let con = await this.rq.postRequest(... request ...).toPromise()
      this.loading = false
      

      if (con.status == 200) {

          this.dados = con.data;
          this.search.setValeu(this.dados)

        }

      }
    } catch (error) {
      this.loading = false

      console.log(error)
    }
  }

我设置了提供商的每个组件中的ChildComponent


  constructor(private trans: SearchService) {

    this.subscription = trans.value$.subscribe(val => {
      console.log(val);
    })


  }

,将服务传递给构造函数等...我不知道这是怎么了。请帮我

i try passa values to child component using subject service, I am based on Documentatio, but somethink is wrong and when a try print data dont show nothing.

i have the navComponent which request api base on select input. When changing select update request and send new data to child page.

Service:



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

@Injectable()
export class SearchService {
    // Observable string sources
    private data = new Subject<any>();

    // Observable string streams
    value$ = this.data.asObservable();

    // Service message commands
    setValeu(value: any) {
        this.data.next(value);
    }


}

NavComponent:

 async consultaIndicadores() {
    try {
      this.loading = true
      let con = await this.rq.postRequest(... request ...).toPromise()
      this.loading = false
      

      if (con.status == 200) {

          this.dados = con.data;
          this.search.setValeu(this.dados)

        }

      }
    } catch (error) {
      this.loading = false

      console.log(error)
    }
  }

childComponent


  constructor(private trans: SearchService) {

    this.subscription = trans.value$.subscribe(val => {
      console.log(val);
    })


  }

in each component i seted provider, pass service to constructor etc... I dont know what its wrong. please help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文