【发布时间】:2019-07-11 18:52:50
【问题描述】:
我无法从订阅返回中为任何 var 赋值,我的 observable 名称是 reportess,我试图在我从外部组件声明 observable 的同一服务中将值赋给 var。另一方面,我可以通过记录订阅的返回来打印。
服务:
import { Injectable, Input } from '@angular/core';
//Archivo json
import _reportes from "../archivos json/reportes.json";
import _vacio from "../archivos json/vacio.json";
//Exportador pdf
import * as jsPDF from 'jspdf';
//firebase
import {AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument} from '@angular/fire/firestore';
import {Observable} from 'rxjs'
import {Subject} from 'rxjs'
@Injectable({
providedIn: 'root'
})
export class ServicesService {
@Input() reportes
coleccionReporte: AngularFirestoreCollection<Reporte>;
reportess: Observable<Reporte[]>;
reportesMargis;
reportesDoc: AngularFirestoreDocument<Reporte>;
ColeccionDeReportes;
Reporte;
constructor(public firebase:AngularFirestore) {
this.reportess = firebase.collection('reporte').valueChanges();
console.log(this.reportes)
}
组件:
constructor(private reportesServices:ServicesService) { }
ngOnInit() {
this.reportesServices.reportess.subscribe(data => {this.reportesServices.reportes = data} ); //dont work
this.reportesServices.reportess.subscribe(data => {console.log(data)} ); //work
}
【问题讨论】:
-
你怎么知道第一个例子行不通?因为
console.log(this.reportes)在ServicesService的最后一行显示了undefined? -
yes 'console.log(this.reportes)' 行显示未定义
标签: angular typescript firebase google-cloud-firestore observable