“[ts] 找不到名称 'Item'。”
"[ts] Cannot find name 'Item'."
我正在按照 here 的说明试用 Firebase Firestore:
在 app.component.ts 文件的以下代码块中,出现以下错误:
[ts] Cannot find name 'Item'.
import { Component } from '@angular/core';
import { AngularFirestore, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-root',
template: `
<div>
{{ (item | async)?.name }}
</div>
`
})
export class AppComponent {
private itemDoc: AngularFirestoreDocument<Item>;
item: Observable<Item>;
constructor(private afs: AngularFirestore) {
this.itemDoc = afs.doc<Item>('items/1');
this.item = this.itemDoc.valueChanges();
}
update(item: Item) {
this.itemDoc.update(item);
}
}
我的问题是,他们在文档中使用的 Item 类型是什么?我应该在某个地方手动声明这种类型吗?还是我漏掉了什么?
Item
是您希望返回的文档类型。例如,假设我正在尝试从 firestore 获取任务文档。
afs.doc<Task>('tasks/1');
这里,Task
是应该返回的类型。您必须手动定义它。
interface Task {
title: string;
description: string;
}
所以在这个例子中,firestore 应该返回一个类似于
的对象
{
title: 'Task 1',
description: 'My first task'
}
由于某些原因,这些文档没有定义项目。它很可能是从某个地方进口的。为简洁起见,他们可能没有将其包含在导入中。
"Item" 确实是您必须定义的接口。我在同一个 .ts 文件中定义了它(但我想我可以通过引用它在不同的文件中正确地完成它...
导入和@Component 之间:
interface MyObjectInterface {
name: string;
...
}
然后在 export class AppComponent 块中
private itemDoc: AngularFirestoreDocument<MyObjectInterface>;
等等
它对我有用,我可以通过这种方式从 firestore 获取数据
我正在按照 here 的说明试用 Firebase Firestore:
在 app.component.ts 文件的以下代码块中,出现以下错误:
[ts] Cannot find name 'Item'.
import { Component } from '@angular/core';
import { AngularFirestore, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-root',
template: `
<div>
{{ (item | async)?.name }}
</div>
`
})
export class AppComponent {
private itemDoc: AngularFirestoreDocument<Item>;
item: Observable<Item>;
constructor(private afs: AngularFirestore) {
this.itemDoc = afs.doc<Item>('items/1');
this.item = this.itemDoc.valueChanges();
}
update(item: Item) {
this.itemDoc.update(item);
}
}
我的问题是,他们在文档中使用的 Item 类型是什么?我应该在某个地方手动声明这种类型吗?还是我漏掉了什么?
Item
是您希望返回的文档类型。例如,假设我正在尝试从 firestore 获取任务文档。
afs.doc<Task>('tasks/1');
这里,Task
是应该返回的类型。您必须手动定义它。
interface Task {
title: string;
description: string;
}
所以在这个例子中,firestore 应该返回一个类似于
的对象{
title: 'Task 1',
description: 'My first task'
}
由于某些原因,这些文档没有定义项目。它很可能是从某个地方进口的。为简洁起见,他们可能没有将其包含在导入中。
"Item" 确实是您必须定义的接口。我在同一个 .ts 文件中定义了它(但我想我可以通过引用它在不同的文件中正确地完成它...
导入和@Component 之间:
interface MyObjectInterface {
name: string;
...
}
然后在 export class AppComponent 块中
private itemDoc: AngularFirestoreDocument<MyObjectInterface>;
等等
它对我有用,我可以通过这种方式从 firestore 获取数据