错误TS2322:类型为"Observable< string []>"不可分配给类型"Observable< Sensors []>" [英] error TS2322: Type 'Observable<string[]>' is not assignable to type 'Observable<Sensors[]>'

查看:203
本文介绍了错误TS2322:类型为"Observable< string []>"不可分配给类型"Observable< Sensors []>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此错误:

错误TS2322:无法将类型可观察"分配给该类型 可观察".类型'string []'不可分配给类型 传感器[]".不能将字符串"类型分配给传感器"类型.

error TS2322: Type 'Observable' is not assignable to type 'Observable'. Type 'string[]' is not assignable to type 'Sensors[]'. Type 'string' is not assignable to type 'Sensors'.

在此打字稿代码中显示:

show in this Typescript Code:

    import { Observable } from 'rxjs/Rx';
    import { map, startWith } from 'rxjs/operators';

    filteredSensors: Observable<Sensors[]>;

        constructor(private fb: FormBuilder, private ws: SensorService
            ) {
                this.myform= new FormGroup({
                  'sensors_id': this.fb.array([], Validators.required),
                  'unit_price': new FormControl('', [Validators.required, Validators.nullValidator]),
                });

                    this.filteredSensors = this.myform.controls['sensors_id'].valueChanges
                  .pipe(
                    startWith(['']),
                  );
              }
        ngOnInit() {
           this.ws.getAllSensors().subscribe(
            sensors => {
            this.sensors = sensors
           }
         );
       }

在html中,我是这样的:*ngFor="let sensor of filteredSensors | async | myPipe: sensors : 'sensor_serial': i"

In html I have this : *ngFor="let sensor of filteredSensors | async | myPipe: sensors : 'sensor_serial': i"

我有这个管道:

    import { Pipe, PipeTransform } from '@angular/core';

    @Pipe({
        name: 'myPipe'
    })
 export class MyPipe implements PipeTransform {
    transform<T>(value: Sensors, list: T[], field: string, idx: number): T[] {
        const filterValue = value[idx] ? value[idx].toString().toLowerCase() : ''; //correct this line of as per Sensors object
        return list.filter(sensor => sensor[field].toLowerCase().indexOf(filterValue) === 0);
    }
}

请问有什么想法,如何解决此错误?

Any idea please, how to solution this error?

Sensors.ts

Sensors.ts

export class Sensors {
    sensorType_id: number;
    sensortype_id: number;
    sensors_id: string;
    sensor_serial: string;
    active: number;
    sensordescription: string;
    unit_price: number;
    description: string;
    note: string;
    Sensor_id: number;
    default_price: number;
    client_id: string;
    client_name: String;

    constructor(obj: any) {

        this.sensorType_id = obj && obj.sensortype_id || obj.sensorType_id;
        this.sensortype_id = obj && obj.sensortype_id;
        this.client_id = obj && obj.client_id;
        this.sensor_serial = obj && obj.sensor_serial;
        this.sensors_id = obj && obj.sensors_id;
        this.active = obj && obj.active;
        this.sensordescription = obj && obj.sensordescription;
        this.unit_price = obj && obj.unit_price;
        this.description = obj && obj.description;
        this.note = obj && obj.note;
        this.Sensor_id = obj && obj.Sensor_id;
        this.default_price = obj && obj.default_price;
        this.client_name = obj && obj.client_name;
    }
}

和ws SensorService:

and ws SensorService:

public getAllSensors(): Observable<Sensors[]> {
    let headers = new Headers();
    headers.append('x-access-token', this.auth.getCurrentUser().token);
    return this.http.get(Api.getUrl(Api.URLS.getAllSensors), {
        headers: headers
    })
        .map((response: Response) => {
            let res = response.json();
            if (res.StatusCode === 1) {
                this.auth.logout();
                return false;
            } else {
                return res.StatusDescription.map(sensors => {
                    return new Sensors(sensors);
                });
            }
        });
}

推荐答案

您的Pipe中存在类型问题.在Pipe中,您说它将在String上运行,但是您正在传递Sensors.因此,更改Pipeargument type并在Pipe内的业务登录中进行更改,

There is type issue in your Pipe. In Pipe, you are saying that it will operate on String however you are passing Sensors. So change the argument type of Pipe and make the changes in you business login inside the Pipe,

 @Pipe({
  name: 'myPipe'
})
export class MyPipe implements PipeTransform {
  transform(items: string[], value: string): any[] {
    if (!items) {
      return [];
    }
    let array =  items.filter(item => item == value);
    return  items.filter(item => item == value);
  }
}

in html

*ngFor="let sensor of filteredSensors | async | myPipe: i"

注意:上面的"i"是将对其应用过滤器的值.

Note : 'i' above is value by which filter will apply on.

工作副本在此处- https://stackblitz.com/edit/angular-阵列上的管道

这篇关于错误TS2322:类型为"Observable&lt; string []&gt;"不可分配给类型"Observable&lt; Sensors []&gt;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆