javascript - 如何在 TypeScript 中使用 Request Promise?

标签 javascript typescript

我已经安装了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/fromrequire 之间的区别。

关于javascript - 如何在 TypeScript 中使用 Request Promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40356379/

相关文章:

http - 如何在Angular2中获取http请求中的状态代码

javascript - 在运行时读取 VSTS 发布变量(TypeScript/Webpack SPA)

javascript - 使用 Highcharts 创建 marimekko 图表

javascript - 滚动到 div 顶部

javascript - Angular Datepicker更改日期格式

reactjs - 如何在 React 中重构我的 App.js 页面路由?最好的做法是什么?

typescript - 运行时未定义枚举类型

javascript - Quasar - 垂直滚动条不能在超过 600 像素的宽度上工作

javascript - 监听游戏 handle 连接和输入

javascript - HTML linter 中的 Microsoft VS Code 和 Angular 2 标签