我有一个Ionic页面,它正在查询FirebaseListObservable,以便在ion-searchbar上进行动态搜索。它适用于 Angularfire2@4.0.0-rc.0 和 firebase@4.5.0。升级新版本AngularFire 5.0后,我遇到了有关FirabaseListObservable的问题,没有从新API导出。
import { Component } from '@angular/core';
import { IonicPage, NavParams, ViewController } from 'ionic-angular';
import {AngularFireDatabase, FirebaseListObservable} from 'angularfire2/database';
import { Observable} from 'rxjs';
import { Response } from '@angular/http';
@IonicPage()
@Component({
selector: 'page-modal-groups',
templateUrl: 'modal-groups.html'
})
export class ModalGroupsPage {
groups: FirebaseListObservable<any>;
constructor(public navParams: NavParams,
public viewCtrl:ViewController,
public afDatabase: AngularFireDatabase) {
}
getItems = (ev: any) : Observable<Response> => {
this.groups = this.afDatabase.list('/Groups', {
query:{
orderByChild: 'namelower',
startAt: (ev.target.value),
endAt: (ev.target.value + 'uf8ff')
}
}
);
return this.groups;
}
chooseGroups(item:any){
this.viewCtrl.dismiss(item);
// console.log(this.product);
}
closeModal(){
this.viewCtrl.dismiss();
}
}
<ion-header>
<ion-navbar>
<ion-title>Grup Seç</ion-title>
<ion-buttons end>
<button ion-button color="danger" (click)="closeModal()" >
Kapat
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-searchbar (ionInput)="getItems($event)"></ion-searchbar>
<ion-list>
<ion-item *ngFor="let item of groups | async" (click)="chooseGroups(item)">
{{ item.name }}
</ion-item>
</ion-list>
<!--<button ion-item *ngFor="let item of products | async" (click)="chooseProduct(item)">
{{item.name}}
</button>-->
</ion-content>
然后我用新的 api 更改了查询,但我无法将响应作为可观察对象返回,如下所示。我收到这样的错误
**消息:"类型'可观察[]>'不可分配给类型'可观察'。 类型"AngularFireAction[]"不能分配给类型"响应"。 属性"type"在类型"AngularFireAction[]"中缺失。**
import { Component } from '@angular/core';
import { IonicPage, NavParams, ViewController } from 'ionic-angular';
import {AngularFireDatabase, AngularFireAction} from 'angularfire2/database';
import { Observable, BehaviorSubject} from 'rxjs';
import { Response } from '@angular/http';
/**
* Generated class for the ModalGroupsPage page.
*
* See http://ionicframework.com/docs/components/#navigation for more info
* on Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-modal-groups',
templateUrl: 'modal-groups.html',
})
export class ModalGroupsPage {
items: Observable<AngularFireAction<firebase.database.DataSnapshot>[]>;
group: BehaviorSubject<any>;
constructor(public navParams: NavParams,
public viewCtrl:ViewController,
public afDatabase: AngularFireDatabase) {
}
getItems = (ev: any) : Observable<Response> => {
this.group = new BehaviorSubject(ev);
this.items = this.group.switchMap(name =>
this.afDatabase.list('/Groups', ref => name ? ref.orderByChild('namelower').startAt(ev.target.value).endAt(ev.target.value + 'uf8ff') : ref
).snapshotChanges());
return this.items;
}
为了从 5.0 开始从 AngularFireList 获得Observable<Response>
,使用valueChanges()
功能。
在此处查看更改。
return this.afDatabase.list('/Groups', {
query:{
orderByChild: 'namelower',
startAt: (ev.target.value),
endAt: (ev.target.value + 'uf8ff')
}
}
).valueChanges();
如果要在 this.groups
中保存 this.afDatabase.list()
的实例,它将AngularFireList
而不是FirebaseListObservable
。
您需要使用如下所示的.valueChanges()
。这是文档。
groups: AngularFireList<any>;
constructor(){}
getItems = (ev: any) : AngularFireList<any> {
this.groups = this.afDatabase.list('/Groups', {
query:{
orderByChild: 'namelower',
startAt: (ev.target.value),
endAt: (ev.target.value + 'uf8ff')
}
}
).valueChanges();
return this.groups;
}
Angular 5.0 是 AngularFireDatabase 的重构,它删除了 FirebaseListObservable 和 FirebaseObjectObservable
FirebaseListObservable ====> AngularFireListFirebaseObjectObservable====>AngularFireObject
您可以点击链接
在 Angularfire2 5.0 或更高版本中,如果您需要带有查询的可观察列表,则必须使用 AngularFireAction,如下所示
import { Component } from '@angular/core';
import { IonicPage, NavParams, ViewController} from 'ionic-angular';
import {AngularFireDatabase, AngularFireAction} from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import {EmptyObservable} from 'rxjs/observable/EmptyObservable';
// import { Response } from '@angular/http';
import * as firebase from 'firebase/app';
@IonicPage()
@Component({
selector: 'page-modal-groups',
templateUrl: 'modal-groups.html',
})
export class ModalGroupsPage {
groups: Observable<AngularFireAction<firebase.database.DataSnapshot>[]>;
constructor(public navParams: NavParams,
public viewCtrl:ViewController,
public afDatabase: AngularFireDatabase) {
}
-
getItems = (ev: any) : Observable<AngularFireAction<firebase.database.DataSnapshot>[]> => {
if(!ev.data){
return this.groups = new EmptyObservable();
}
this.groups = this.afDatabase.list('/Groups', ref => ref.orderByChild('namelower').startAt(ev.target.value).endAt(ev.target.value + 'uf8ff')).valueChanges();
// this.groups = this.groupsRef.valueChanges();
return this.groups;
}
chooseGroups(item:any){
// this.product.push({key:item.$key, name:item.name, price:item.price, quantity:1 });
this.viewCtrl.dismiss(item);
// console.log(this.product);
}
closeModal(){
this.viewCtrl.dismiss();
}
}
我安装了以下版本的 angularfire2 和 Firebase:
npm install angularfire2@4.0.0-rc0 firebase --save