无法在angularFire应用程序中使用查询

时间:2017-10-15 09:11:53

标签: angular angularfire2

我正在创建一个AngularFire应用,这是我的app.component.ts中的代码:

import { Component } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 
'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase/app';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  user: Observable<firebase.User>;
  items: FirebaseListObservable<any[]>;
  msgVal: string = '';

  constructor(public afAuth: AngularFireAuth, public af: AngularFireDatabase) {
    this.items = af.list('/messages', {
      query: {
        //I am using a limitToLast: 50 here, but isn't working.
      }
    });

    this.user = this.afAuth.authState;

  }
}

问题是它没有编译,我的文本编辑器(vs代码)返回以下错误: 当我将鼠标悬停在query: {上时:[ts] Argument of type '{ query: { limitToLast: number; }; }' is not assignable to parameter of type 'QueryFn'. Object literal may only specify known properties, and 'query' does not exist in type 'QueryFn'.

我似乎无法找到问题,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:2)

我猜你使用的是最后一个angularfire2和firebase版本,因为我有同样的例外。他们用最新的更新改变了一些事情。这是指向angularfire2 latest docs

的链接

在您的情况下,您可以像这样修复它:

constructor(public afAuth: AngularFireAuth, public af: AngularFireDatabase) {
   this.items = af.list('/messages', ref => ref.orderByKey(true).limitToLast(50));
});

   this.user = this.afAuth.authState;

}