Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用服务以角度显示可观察数据_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 如何使用服务以角度显示可观察数据

Javascript 如何使用服务以角度显示可观察数据,javascript,angular,typescript,Javascript,Angular,Typescript,home.component.ts report.service.ts 这里的问题是它没有显示可观察的数据。运行应用程序时没有错误,但它不显示displayAlert$、reportsToday$、alertsToday$和alertsdayy$的数据 我使用它是因为,我将从其他组件使用它 如何从服务中获取可观测数据并将其显示在主组件上?在组件中,您无法直接从服务中看到变量。它应该来自组件本身 此外,您还需要订阅使用.next()触发的值。除非您订阅它们,否则不会设置值。在组件中,您无法直接从服

home.component.ts

report.service.ts

这里的问题是它没有显示可观察的数据。运行应用程序时没有错误,但它不显示
displayAlert$、reportsToday$、alertsToday$和alertsdayy$
的数据

我使用它是因为,我将从其他组件使用它


如何从服务中获取可观测数据并将其显示在主组件上?

在组件中,您无法直接从服务中看到变量。它应该来自组件本身


此外,您还需要订阅使用
.next()
触发的值。除非您订阅它们,否则不会设置值。

在组件中,您无法直接从服务中看到变量。它应该来自组件本身


此外,您还需要订阅使用
.next()
触发的值。除非您订阅它们,否则不会设置值。

我认为
新的行为主体(未定义)是导致问题的原因。
你能像
一样初始化新的行为主体([])或如果它是字符串,则
新行为主体(“”)和if number then
新行为主体(0)


我假设您已经正确地从组件调用了服务。

我认为
新的行为主体(未定义)是导致问题的原因。
你能像
一样初始化新的行为主体([])或如果它是字符串,则
新行为主体(“”)和if number then
新行为主体(0)

我假设您已经正确地从组件调用了服务

<h1>{{ (reportsToday$ | async)}}</h1>
<div echarts [options]="alertsDaily$ | async">
<div echarts [options]="alertsToday$ | async">
<div [onDisplay]="alertsDaily$ | async">
constructor(private report: ReportService) {}

getReports() {
  this.report.getStatusReport();
}
  displayAlert$ = new BehaviorSubject(undefined);
  reportsToday$ = new BehaviorSubject(undefined);
  alertsToday$ = new BehaviorSubject(undefined);
  alertsDaily$ = new BehaviorSubject(undefined);

constructor() {}

getStatusReport() {

  loading = {
    today: false,
    daily: false
  };

    this.loading.today = true;
    this.loading.daily = true;

    const severities = ['LOW', 'MEDIUM', 'HIGH', 'URGENT'];
    const reportModules = [
      { url: '', params: { to: format(TODAY, DATE_FORMAT).toString(), from: format(TODAY, DATE_FORMAT).toString() } },
      {
        url: 'application1',
        params: { to: format(TODAY, DATE_FORMAT).toString(), from: format(TODAY, DATE_FORMAT).toString() }
      },
      {
        url: 'application2',
        params: {
          to: format(endOfWeek(TODAY), DATE_FORMAT).toString(),
          from: format(startOfWeek(TODAY), DATE_FORMAT).toString()
        }
      },
      {
        url: 'application3',
        params: {
          to: format(endOfWeek(TODAY), DATE_FORMAT).toString(),
          from: format(startOfWeek(TODAY), DATE_FORMAT).toString()
        }
      }
    ];

    const promises = applicationModule.map(
      target =>
        new Promise(resolve => {
          this.notificationService
            .getSummary(target.url, target.params)
            .pipe(take(1))
            .subscribe(
              (result: Response) => 
                resolve({ target, result });
              },
              (err: Error) => {
                // return reject(err);
              }
            );
        })
    );

    Promise.all(promises).then(results => {
      const options = this.preConfig;
      const week = this.getWeek();

      results.forEach((res: any) => {
             ....

        if (res.target.url !== '') {
          if (res.target.url === 'application1') {
            ....
            this.loading.today = false;
            this.alertsToday$.next(options.today);
          }else {

            if (res.target.url === 'application2') {
              ...
              ...
              this.loading.daily = false;
              this.alertsDaily$.next(options.daily);
            } else {
              ....
              ....
              this.loading.daily = false;
              this.alertsDaily$.next(options.daily);
            }

          }
       }else {
          this.reportsToday$.next(res.result[0]);
}
    }
   });

}