簡體   English   中英

如何在TypeScript中安裝/導入React

[英]How to install/import React in TypeScript

我嘗試開始使用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?

React庫沒有默認導出。 因此,當您要求它import React ,它將在該庫中查找默認導出。 不幸的是,它不存在。 而是以這種方式導入整個內容和名稱空間:

import * as React from 'react';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM