【问题标题】:How to install/import React in TypeScript如何在 TypeScript 中安装/导入 React
【发布时间】:2016-09-26 04:58:42
【问题描述】:

我尝试从TypeScriptReact 开始。由于 React 是用 JavaScript 编写的,所以我使用 npm 安装它

npm install --save react

我需要 React 的类型定义。所以我尝试了

$ typings install react --save
typings ERR! message Unable to find "react" ("npm") in the registry. Did you want to try searching another source? Also, if you want contribute these typings, please help us: https://github.com/typings/registry
typings ERR! caused by https://api.typings.org/entries/npm/react/versions/latest responded with 404, expected it to equal 200

我跑了typings search react,发现

NAME   SOURCE  HOMEPAGE                          DESCRIPTION  VERSIONS  UPDATED
react  dt      http://facebook.github.io/react/               2         2016-05-26T13:46:01.000Z

因此,我明确指定了来源

$ typings i dt~react --save
typings ERR! message Attempted to compile "react" as an external module, but it looks like a global module.

我可以全局安装 React

typings install dt~react --save --global

但是当我尝试导入 React 时,Atom 仍然抱怨 Cannot find module 'react'

import React from 'react'

如何安装 React 或配置 TypeScript 以便在 TypeScript 中导入 React?

【问题讨论】:

    标签: reactjs typescript atom-editor typescript-typings


    【解决方案1】:

    React 库没有默认导出。因此,当您向import React 询问时,它会在该库中查找默认导出。不幸的是,它不存在。相反,以这种方式导入整个内容和命名空间:

    import * as React from 'react';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-09
      • 2016-05-04
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      相关资源
      最近更新 更多