gpt4 book ai didi

Firebase AngularFire2从数据库获取对象

转载 作者:行者123 更新时间:2023-12-02 19:59:54 25 4
gpt4 key购买 nike

我在使用从 firebase 数据库获取的对象时遇到问题。我可以毫无问题地接收 json 文件,如下图所示。 https://i.gyazo.com/6c1c69aca47f92a4e1029bb019042ab2.png

<h1>{{ item | async | json}}</h1>

上面的代码在/src/app/app.component.html ,

这从/src/app/app.component.ts 接收项目对象

import { Component } from '@angular/core';
import { AngularFire, FirebaseObjectObservable } from 'angularfire2';

@Component({
moduleId: module.id,
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class AppComponent {
item: FirebaseObjectObservable<any>;
constructor(af: AngularFire) {
this.item = af.database.object('/releases/');
}
}

我也尝试过使用 item.name.$value 但它不起作用。我想获取 json 文件中的值。并能够在网站中使用它们。

最佳答案

首先,在搜索对象时不需要开始和结束斜杠,这将起作用:

af.database.object('releases')

接下来,您不需要 json 管道,因为 firebase 对象已经采用 json 表示法。您的 html 可能如下所示:

<h1>{{ item | async }}</h1>

但是,通常情况下,您不是直接在模板上使用 Firebase 异步对象,而是将其传递到演示组件(也称为dumb组件)中。演示组件不需要了解有关对象的异步行为的任何信息,它只需要处理如何生成 html。这是处理模板中的异步对象时的常见模式。

所以你的 html 将变成:

<my-child-component [item]="item | async">

子组件:

@Component({
selector: 'my-child-component',
template: '<h1>{{ item }}</h1>'
})
export class MyChildComponent {
@Input() item: any;
...

关于Firebase AngularFire2从数据库获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38761641/

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