Javascript Typescript 模块没有导出成员 - 反应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37146858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:59:07  来源:igfitidea点击:

Typescript module has no exported members - react

javascriptreactjstypescriptredux

提问by user2302244

I am working on a react, redux Typescript application. I have a strange situation where after some changes one of the modules has stopped exporting its members.

我正在开发一个 react、redux Typescript 应用程序。我有一个奇怪的情况,经过一些更改后,其中一个模块停止导出其成员。

The folder structure is:

文件夹结构为:

src
|---- components
|---- containers

The 'components' folder has the .tsx files while the wrapping .ts files are in the 'containers' folder.

“components”文件夹包含 .tsx 文件,而包装 .ts 文件位于“containers”文件夹中。

The module NodeList.tsx is listed below:

下面列出了模块 NodeList.tsx:

import * as React from "react";

export const NodeList = (props) => (
    <div id="NodeList">
        <ul>
        {props.items.map((item, i) => (
            <li key={i} >
                <span id={"Item_"+i}>{item}</span>
            </li>
            )
        )}
        </ul>
    </div>
    )

The wrapping container NodeListContainer is:

包装容器 NodeListContainer 是:

import { connect } from "react-redux";
import { Provider } from "react-redux";

import { Nodelist } from "../components/NodeList"

const nodesAsArrayOfType = (state, nodeType: string) => {
    console.log("Going for nodes of type ", nodeType)
    let a = state.data.model.nodes
    let k = Object.keys(a);
    let sub = k.filter((e) => {
                   return a[e].nodeType == nodeType
        }).map((e) => a[e])
    console.log("Got nodes ", sub)    
    return sub
}

const mapStateToProps = (state) => {
    var list = nodesAsArrayOfType(state, state.UIstate.focusNodeType).map((e) => {return JSON.stringify(e)})
    console.log("Returning ", list, list.length)
    return {
        items: list
    }
}

const mapDispatchToProps = (dispatch) => {
    return {
        newTextValue: (e) => {dispatch({type: "ON_CHANGE", text: e.target.value})}
    }
}

export const NodeListContainer = connect(
    mapStateToProps,
    mapDispatchToProps
    )(NodeList)

The import of NodeList above is being flagged with the error message

上面的 NodeList 的导入被标记为错误消息

ERROR in ./src/containers/NodeListContainer.ts
(4,10): error TS2305: Module '"MyProject/src/components/NodeList"' has no exported member 'Nodelist'.

Can anyone provide any insight into what might have happened here?

任何人都可以对这里可能发生的事情提供任何见解吗?

回答by drinchev

Your code should work if you fix your typo.

如果你修正了你的错字,你的代码应该可以工作。

Instead of

代替

import { Nodelist } from "../components/NodeList"

you should write :

你应该写:

import { NodeList } from "../components/NodeList"
//           ^ capital L

回答by kerosenekiwi

If anyone is still having this problem, I found that I was only exporting one item from the file so changing

如果有人仍然遇到此问题,我发现我只从文件中导出一项,因此更改

export default function App() {
   ...
};

to

export function App() {
   ...
}

seemed to do the trick!

似乎成功了!

回答by Jon

Another thing to check is ambiguous / similar file names.

要检查的另一件事是不明确/相似的文件名。

This can also happen if you have two files in the same package whose name differs only by the extension. For example if you have

如果同一个包中有两个文件的名称仅因扩展名不同,也会发生这种情况。例如,如果你有

folder
      foo.tsx
      foo.ts

When you do the following import:

当您执行以下导入时:

import { Something } from "./foo";

It will only work with items from one of the files.

它仅适用于其中一个文件中的项目。

The solution in this case is to rename one of the files to remove the ambiguity. Then the imports will work just fine.

这种情况下的解决方案是重命名其中一个文件以消除歧义。然后进口将工作得很好。

回答by Salah Osman

Avoid having two different file extensions such as 'js' and 'txs' in similar project scope/folders otherwise it will lead to type-errors such as this.

避免在类似的项目范围/文件夹中使用两个不同的文件扩展名,例如“js”和“txs”,否则会导致诸如此类的类型错误。