Angular 可观察的<;任何[]>;数组[]
我正在尝试使用管道转换数据。管道在html中工作良好,但在组件中使用时不希望工作。我得到以下错误: “Observable”类型的参数不能分配给“any[]”类型的参数。类型“Observable”中缺少属性“includes” 组件1.ts:Angular 可观察的<;任何[]>;数组[],angular,Angular,我正在尝试使用管道转换数据。管道在html中工作良好,但在组件中使用时不希望工作。我得到以下错误: “Observable”类型的参数不能分配给“any[]”类型的参数。类型“Observable”中缺少属性“includes” 组件1.ts: this.locationChangeForUserObservable = fucareDatabaseService.getLocationChangesForUser('user1' + '/locationChange'); this.list
this.locationChangeForUserObservable = fucareDatabaseService.getLocationChangesForUser('user1' + '/locationChange');
this.list = new WeekFilterPipe().transform(this.locationChangeForUserObservable, 'datum', this.mondayInMS);
this.list = new CalculateWeekPipe().transform(this.locationChangeForUserObservable, this.mondayInMS);
我的烟斗:
@Pipe({
name: 'weekFilter'
})
@Injectable()
export class WeekFilterPipe implements PipeTransform {
transform(items: any[], field: string, value: string): any[] {
if (!items) {
return [];
}
if (!field || !value) {
return items;
}
var monday = moment(value);
var tuesday = moment(monday).add('24', 'hours');
var wednesday = moment(monday).add('48', 'hours');
var thursday = moment(monday).add('72', 'hours');
var friday = moment(monday).add('96', 'hours');
var saturday = moment(monday).add('120', 'hours');
var sunday = moment(monday).add('144', 'hours');
monday = monday.format('DD-MM-YYYY');
tuesday = tuesday.format('DD-MM-YYYY');
wednesday = wednesday.format('DD-MM-YYYY');
thursday = thursday.format('DD-MM-YYYY');
friday = friday.format('DD-MM-YYYY');
saturday = saturday.format('DD-MM-YYYY');
sunday = sunday.format('DD-MM-YYYY');
return items.filter(item => {
if (item[field].includes(monday) || item[field].includes(tuesday) ||
item[field].includes(wednesday) || item[field].includes(thursday)
|| item[field].includes(friday) || item[field].includes(saturday) || item[field].includes(sunday)) {
return true;
}
});
}
@Pipe({
name: 'calculateWeekPipe'
})
@Injectable()
export class CalculateWeekPipe implements PipeTransform {
transformDate(datum) {
var temp = datum.split(' ')[0];
var temp = temp.split('-');
return temp.reverse().join('-') + ' ' + datum.split(' ')[1];
}
getDay(item) {
var datum = item.datum;
if (typeof datum === 'undefined') {
console.log('damn');
}
...
非常感谢您的帮助
提前感谢。您无法将可观测值转换为数组,因为它是异步的,但您可以订阅可观测值并将发出的值传递给管道函数:
this.locationChangeForUserObservable.subscribe(locationChanges => {
this.list = new CalculateWeekPipe().transform(locationChanges , this.mondayInMS);
});
比如说
当然,如果这个可观测值精确地返回需要传递给管道函数的数组。否则,您可能需要一些转换(
map
,toArray
可能,取决于实际发出的值)当transform
函数需要一个数组时,错误表明您正在传入一个可观察值this.list=new WeekFilterPipe().transform(this.locationChangeForUserObservable…
在这里,您可以看到您正在传入一个observable作为第一个参数,但它应该是一个数组。observablelocationChangeForUserObservable
是否返回需要传递给转换方法的数组?@LLai locationChangeForUserObservable从该方法获取其值:GetLocationChangesForUserObservable=this.db.list('users/'+user).valueChanges();返回this.locationChangeForUserObservable;}但是我不知道如何将其转换为数组。答案是:你需要订阅可观测数据,将返回的数据转换为数组(如果需要),然后运行转换方法