我正在尝试使用管道转换数据。管道在html中运行良好,但在组件中使用时不想工作。我收到以下错误: '类型'Observable'的参数不能分配给'any []'类型的参数。 'Observable'类型中缺少属性'includes'。'
Component.ts:
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');
}
...
非常感谢任何帮助!
提前致谢。
答案 0 :(得分:1)
您无法将Observable转换为Array,因为它是异步的,但您可以订阅observable并将发出的值传递给管道函数:
this.locationChangeForUserObservable.subscribe(locationChanges => {
this.list = new CalculateWeekPipe().transform(locationChanges , this.mondayInMS);
});
例如。
当然,如果这个observable正好返回你需要传递给管道函数的数组。否则,您可能需要进行一些转换(map
,toArray
可能,取决于实际发出的值)