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