【发布时间】:2018-12-27 21:11:51
【问题描述】:
我已使用Angular Heroes tutorial 并正在尝试实施 pouchdb 服务来提供数据。我已经备份了原始的英雄服务,它提供了一系列具有不同属性的英雄。我现在正在尝试修改服务以在本地从 pouchdb 中提取文档。我修改了 HeroService 类,并在构造函数中设置了一些选项。
import { Injectable, EventEmitter } from '@angular/core';
import {Hero} from './hero';
import {HEROES} from './mock-heroes';
import { Observable, of } from 'rxjs';
import {MessageService} from './message.service';
import PouchDB from 'pouchdb';
@Injectable({
providedIn: 'root'
})
export class HeroService {
private isInstantiated: boolean;
private database: any;
private listener: EventEmitter<any> = new EventEmitter();
constructor(private messageService:MessageService) {
if(!this.isInstantiated) {
this.database = new PouchDB("http://server:5984/db1");
this.isInstantiated = true;
}
}
getHeroes(): Observable<Hero[]> {
console.log(this.database.allDocs({include_docs: true}))
return of(this.database.allDocs({include_docs: true}));
this.messageService.add("HeroService: fetched heroes");
};
console.log 在 hero.service.ts 文件的 console.log 行中显示“ZoneAwarePromise”,但包含我想在屏幕上显示的数据和文档数组。我是否采取了正确的方法,使用服务将 PouchDB 中的数据提供给 Angular 应用程序?
我试图在其中显示此数据的组件是“dashboard.components.ts”文件,这是新功能:
getHeroes(): void {
console.log(this.heroes)
console.log(this.heroService.getHeroes());
this.heroService.getHeroes()
.subscribe(heroes => this.heroes = heroes);
//console.log(this)
}
有没有更好的方法来解决这个问题?
【问题讨论】: