我已经安装了request-promise库并尝试在我的 TypeScript 应用程序中使用它,但运气不佳。
如果我这样使用它:
import {RequestPromise} from'request-promise';
RequestPromise('http://www.google.com')
.then(function (htmlString) {
// Process html...
})
.catch(function (err) {
// Crawling failed...
});
我在 TS 编译输出中看到了这个:
error TS2304: Cannot find name 'RequestPromise'.
如果我这样使用它:
import * as rp from'request-promise';
rp('http://www.google.com')
.then(function (htmlString) {
// Process html...
})
.catch(function (err) {
// Crawling failed...
});
我看到一个错误,指出对象 rp 上没有“.then()”方法。
如何将它与 TypeScript 一起正确使用?
最佳答案
您必须导入所有 (*
) 而不仅仅是 RequestPromise
:
import * as request from "request-promise";
request.get(...);
This answer详细说明了 import/from
和 require
之间的区别。
关于javascript - 如何在 TypeScript 中使用 Request Promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40356379/