gpt4 book ai didi

html - Firebase Cloud Firestore 无法加载数据

转载 作者:太空狗 更新时间:2023-10-29 15:58:52 25 4
gpt4 key购买 nike

当我打开 web 数据时显示但我更改组件并返回此组件数据不显示。它不显示任何项目。我检查日志但没有运行到 ngOnIt。 ngOnIt 在 web strat 或 web reload 上运行。

这是 stock.service.ts

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

import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';

import { Observable } from 'rxjs/Observable';
import { Stock } from './stock.model';

@Injectable()
export class StockService {

stocksCollection: AngularFirestoreCollection<Stock>;
stocks: Observable<Stock[]>;

constructor(public afs: AngularFirestore) {
this.stocks = this.afs.collection('stocks').valueChanges();
}
getStock(){
return this.stocks;
}
}

股票.component.ts

import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute,Params } from '@angular/router';

import { StockService } from '../shared/stock.service';
import { Stock } from '../shared/stock.model';
import { Brands } from '../shared/brand';

import { Subscription } from 'rxjs/Subscription';

@Component({
selector: 'app-stock',
templateUrl: './stock.component.html',
styleUrls: ['./stock.component.css']
})
export class StockComponent implements OnInit {

stocks: Stock[];
brand: Brands;
sub: Subscription;
brand_name: string;

constructor(
private stockService: StockService,
private router: ActivatedRoute
) { }

ngOnInit() {
this.stockService.getStock().subscribe(stocks => {
console.log(stocks);
this.stocks = stocks;
});

this.sub = this.router.params.subscribe(params =>{
this.brand_name = params['brand'];
});

console.log('Brand : '+this.brand_name);
}

}

和 stock.component.html

<div *ngIf="stocks != null || stocks?.length > 0 ; else noStocks" >
<ul *ngFor="let item of stocks" class="collection">
<li class="collection-item " >{{item.name}} | {{item.brand}} | {{item.price}}</li>
</ul>
</div>

<ng-template #noStocks>
<p>no item</p>
</ng-template>

我不知道代码哪里错了。谢谢你的回答。

最佳答案

不要在服务的构造函数中加载,将其移动到方法中

  constructor(public afs: AngularFirestore) {

}
getStock(){
this.stocks = this.afs.collection('stocks').valueChanges();
return this.stocks;
}

关于html - Firebase Cloud Firestore 无法加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49083813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com