Return 数组<custom class> 来自方法
Return array<custom class> from method
好的,所以我调用 firebase 和 return children 给定位置。
然后我遍历 returned 值并填充我的自定义 class,即 description
和 id
这有效,因为我记录 options
到控制台,我可以看到条目。
我遇到的问题是尝试 return 将此方法中的值传递给我的组件,因为代码如下所示:
getIntent(): Promise<Array<Dropdown>> {
const options: Dropdown[] = [];
var d = this.af.database.list('/option1').subscribe(items => {
items.forEach(item => {
options.push({
id: item.$key,
description: item.$value
})
return false;
});
});
return options;
}
在 return 行上,当前带有下划线的错误如下:
Type 'Dropdown[]' is not assignable to type 'Promise<Dropdown[]>'.
Property '[Symbol.toStringTag]' is missing in type 'Dropdown[]'.
我试过:
return options as Promise<Array<Dropdown>>;
但遗憾的是,这没有用,有人可以阐明我如何将此功能用于 return 下拉选项。
旁注:目前也在使用 angularfire2
import 'rxjs/add/operator/map';
getIntent(): Observable<Dropdown[]> {
return this.af.database.list('/option1')
.map(items => items
.map(({$key, $value}) => ({
id: $key,
description: $value
})
);
}
然后您将在某些组件中订阅此方法。
还值得注意的是,虽然您的问题建议使用名为 Dropdown
的 class
,但我们并未对其进行实例化。如果 Dropdown
确实被声明为 class
,最好将其更改为 interface
,这样就不可能意外执行 instanceof
检查是假的。
之前
export class Dropdown {
id: string; // maybe number?
description: string;
}
之后
export interface Dropdown {
id: string; // maybe number?
description: string;
}
好的,所以我调用 firebase 和 return children 给定位置。
然后我遍历 returned 值并填充我的自定义 class,即 description
和 id
这有效,因为我记录 options
到控制台,我可以看到条目。
我遇到的问题是尝试 return 将此方法中的值传递给我的组件,因为代码如下所示:
getIntent(): Promise<Array<Dropdown>> {
const options: Dropdown[] = [];
var d = this.af.database.list('/option1').subscribe(items => {
items.forEach(item => {
options.push({
id: item.$key,
description: item.$value
})
return false;
});
});
return options;
}
在 return 行上,当前带有下划线的错误如下:
Type 'Dropdown[]' is not assignable to type 'Promise<Dropdown[]>'.
Property '[Symbol.toStringTag]' is missing in type 'Dropdown[]'.
我试过:
return options as Promise<Array<Dropdown>>;
但遗憾的是,这没有用,有人可以阐明我如何将此功能用于 return 下拉选项。
旁注:目前也在使用 angularfire2
import 'rxjs/add/operator/map';
getIntent(): Observable<Dropdown[]> {
return this.af.database.list('/option1')
.map(items => items
.map(({$key, $value}) => ({
id: $key,
description: $value
})
);
}
然后您将在某些组件中订阅此方法。
还值得注意的是,虽然您的问题建议使用名为 Dropdown
的 class
,但我们并未对其进行实例化。如果 Dropdown
确实被声明为 class
,最好将其更改为 interface
,这样就不可能意外执行 instanceof
检查是假的。
之前
export class Dropdown {
id: string; // maybe number?
description: string;
}
之后
export interface Dropdown {
id: string; // maybe number?
description: string;
}