简体   繁体   English

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

[英]Typescript module has no exported members - react

I am working on a react, redux Typescript application.我正在开发一个 react、redux Typescript 应用程序。 I have a strange situation where after some changes one of the modules has stopped exporting its members.我有一个奇怪的情况,经过一些更改后,其中一个模块停止导出其成员。

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?任何人都可以对这里可能发生的事情提供任何见解吗?

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

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!似乎成功了!

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.然后进口将工作得很好。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM