我正在编写一个基于 Angular2 的移动应用程序,将 Typescript 与 Nativescript 运行时一起使用,但我面临着 Promises 的一些问题。我有一个 HomeComponent,我希望能够从中调用各种函数(包装在 promises 中),其中之一是 scan
promise 方法。见下文:
BLE 实用类:
export class ble {
scan() {
return new Promise((resolve, reject) => {
try {
// my code emitted
}
catch (e) {
reject(e);
}
});
}
}
Angular2 主页组件:
import {ble} from "../../Utilities/newBLEDevice";
export class HomePage {
_ble: ble = new ble;
bluetoothAdd() {
this._ble.scan.then( // <- ERROR LINE
}
}
但是,当我这样做时,this._ble.scan.then
行出现错误:
[ts] Property 'then' does not exist on type '() => Promise<{}>'
我做错了什么?
最佳答案
该错误消息告诉您您正在尝试访问函数的属性。您不应该尝试在函数本身上运行 then
。您需要调用该函数并对生成的 Promise 使用 then
。改变这个:
this._ble.scan.then(...
为此:
this._ble.scan().then(
关于javascript - Promise 不在 Typescript 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37492888/