类型'{Property:string,Property2:string}不可分配给Observable <filters []>类型

时间:2018-07-02 12:28:39

标签: angular typescript observable

我有一个方法:

getFilters(): Observable<Filters[]> {
    let filters: Observable<Filters[]> = [
      {
        property: "Value",
        property2: "Value2"
     },
     {
       property: "Value3",
       property2: "Value4"
     }
  return filters;
}

我得到一个错误:

  

类型'{属性:字符串,属性2:字符串}不可分配给类型   可观察的。

3 个答案:

答案 0 :(得分:5)

您需要创建一个Observable:

CREATE INDEX ON "Webapp_sensordata" (type_id, machine_id, timestamp);

答案 1 :(得分:0)

这是可观察的工作方式:

getFilters(): Observable<Filters> {
    let filters = [
      {
        property: "Value",
        property2: "Value2"
     },
     {
       property: "Value3",
       property2: "Value4"
     }
    ];
  return Observable.from(filters);
}

您可以像这样使用它:

// Prints out each item
let subscription = this.getFilters().subscribe(
x => console.log('onNext: %s', x),
e => console.log('onError: %s', e),
() => console.log('onCompleted'));

// => onNext: {property: "Value",property2: "Value2"}
// => onNext: {property: "Value3",property2: "Value4"}

有关更多详细信息,请参见this

答案 2 :(得分:0)

我通过添加导入来使其工作:

import { Observable, of } from "rxjs";

然后做

return of(filters);