找不到模块:错误:无法使用打字稿解析“react-select”

问题描述 投票:0回答:5

模块导入对我不起作用。我尝试了堆栈溢出和其他提供的不同解决方案。 这些是我遵循反应选择的步骤

npm install --save @types/react-select

像这样导入模块

import Select from "react-select"

但是我收到了错误

找不到模块:错误:无法解析“react-select”

import * as React from "react";
import ReactSelect from 'react-select'
export class Select extends extends React.Component<someProps>{

render(){
   return(
  <Select id="color" options={options} />
);
}
}

但我找不到解决这个问题的方法。

reactjs typescript react-select
5个回答
9
投票

我通过

npm i react-select
得到了解决方案。


4
投票

您有一个错误:

npm install --save @type/react-select

而不是

npm install --save @types/react-select

此外,我建议使用

--save-dev
而不是
--save
,因为您不需要在生产中进行打字。


2
投票

我发现问题在这里。问题是

@types/react-select
react-select
都添加到了
package.json


0
投票

我的错误有点不同,因为它涉及react-select-async-paginate。

所以就我而言,我必须使用,

npm install react-select react-select-async-paginate

yarn add react-select react-select-async-paginate

希望这对某人有帮助:)


0
投票

就我而言,问题是我正在从事的项目使用旧版本的

react-select
。在版本 5 中,
react-select
被用 typescript 重写,类型包 (
@types/react-select
) 成为一个存根包,其中没有实际的类型定义。

因此,如果您在

react-select
版本 3.2.0 中使用 <5, you have to specify the version of the types definitions package, that matches (or is close to) the version you are using. For example, if your
react-select
,则
package.json
中的定义将是:

"devDependencies": {
    "@types/react-select": "^3.1.2"
  }

有关软件包的可用版本,请访问 https://www.npmjs.com/package/@types/react-select?activeTab=versions

© www.soinside.com 2019 - 2024. All rights reserved.