angular - 错误 TS2322:类型“可观察”' 不可分配给类型 'Observable'
问题描述
这个错误:
错误 TS2322:类型“Observable”不可分配给类型“Observable”。类型“string[]”不可分配给类型“Sensors[]”。类型“字符串”不可分配给类型“传感器”。
在此打字稿代码中显示:
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"
我有这个管道:
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);
}
}
任何想法请,如何解决这个错误?
传感器.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:
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
。因此,更改并在您的业务登录中进行argument
type
更改,Pipe
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);
}
}
在 HTML 中
*ngFor="let sensor of filteredSensors | async | myPipe: i"
注意:上面的“i”是应用过滤器的值。
推荐阅读
- excel - 是否有在 excel vba 消息框中使用 vba 语法来强制用户单击“确定”或关闭 excel?
- android - 如何从笑脸评级栏获取当前选择?
- c - 如何使用 MinGW 使用 ssl 编译 libcurl
- php - 我怎样才能让这个 Php WP_Query 只返回一次类别?
- r - 忽略不符合条件的值,按条件按组密集排名或编号
- html - 仅在 Internet Explorer 上输入表单输入键不会触发提交
- javascript - AWS S3 使用预签名 URL 进行身份验证的用户访问?
- php - 如何使用 ODBC 方法将 PHP 连接到 DBF 文件
- firebase - 在不启用 App Engine 的情况下使用 Firestore
- sql - 执行联接后从 SQL Server 中的 XML 中提取信息