如何使用 AngularFire 创建(设置)数据到 Firestore?
我正在关注 AngularFire 快速入门教程 。我让 Read 开始工作,它在 Angular 视图中显示我的 Firestore 数据库中的数据。我很困惑如何让 Create (set
) 工作。
我的 app.component.html
显示一个带有 Submit
按钮的小文本表单字段,并显示 Firestore 数据库中的值:
<form (ngSubmit)="onSubmit()">
<input type="text" [(ngModel)]="name" name="name">
<button type="submit" value="Submit" >Submit</button>
</form>
<ul>
<li class="text" *ngFor="let item of items | async">
{{item.name}}
</li>
</ul>
我的 app.component.ts
文件有一个函数 onSubmit()
,当用户单击 Submit
按钮时执行。它记录用户在文本表单字段中输入的名称。下一行抛出此错误:
TS2339: Property 'firestore' does not exist on type 'AppComponent'.
23 this.firestore.collection('items').doc('item').set({name: this.name}); // error is here
这是我的 app.component.ts
:
import { Component } from '@angular/core';
import { AngularFirestore } from '@angular/fire/compat/firestore';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
items: Observable<any[]>;
constructor(firestore: AngularFirestore) {
this.items = firestore.collection('items').valueChanges();
}
name: string | null = null;
onSubmit() {
console.log(this.name);
this.firestore.collection('items').doc('item').set({name: this.name}); // error is here
return // return what?
}
}
这与 英雄之旅教程:
export class HeroService {
constructor(private logger: Logger) { }
getHeroes() {
this.logger.log('Getting heroes ...');
return HEROES;
}
}
我的app.module.ts
:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
// Firebase
import { AngularFireModule } from '@angular/fire/compat';
import { AngularFirestoreModule } from '@angular/fire/compat/firestore';
import { environment } from '../environments/environment';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFirestoreModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
视图:
以及我的 Firestore 数据库:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这里的问题与 TypeScript 有关。我相信在这种情况下,最佳实践是在将其注入到组件的构造函数中时,在
firestore: Firestore
前面使用private
访问修饰符,或者您正在使用它的服务,在您的情况下是app.component.ts。private
访问修饰符做了几件事:firestore: any;
中声明了该属性I think the issue here is related to TypeScript. I believe best-practice, in this case, would be for you to use the
private
access modifier in front offirestore: Firestore
while injecting it into the constructor of the component or service where you are using it, in your case app.component.ts.The
private
access modifier does a couple things:firestore: any;
)史蒂夫的答案有效,但我不明白为什么它有效。玩弄一下,这三个都有效:
这不起作用:
根据 TypeScript 手册,
这显然是错误的。
Read
函数无需指定public
、private
或protected
即可工作,但Update
> 函数仅在指定public
、private
或protected
时才起作用。我将尝试使用
Create
和Delete
。然后我将发布有关此 TypeScript 行为的另一个问题。Steve's answer works but I don't understand why it works. Playing around, all three of these work:
This doesn't work:
According to the TypeScript handbook,
That's clearly wrong. The
Read
function works without specifyingpublic
,private
, orprotected
, but theUpdate
function only works whenpublic
,private
, orprotected
is specified.I'll play around with
Create
andDelete
. Then I'll post another question about this TypeScript behavior.