Javascript 正在尝试生成react库,但不断获取错误:元素类型无效:应为字符串
大家好,我正在制作我的第一个React库: 这是: 但当我尝试npm安装它时,我得到了错误:Javascript 正在尝试生成react库,但不断获取错误:元素类型无效:应为字符串,javascript,reactjs,Javascript,Reactjs,大家好,我正在制作我的第一个React库: 这是: 但当我尝试npm安装它时,我得到了错误: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
我不知道下一步要更改什么我尝试更改在index.ts中添加默认导出
像这样:
import ContextMenu from './components/ContextMenu';
export default ContextMenu;
但它仍然没有按预期工作,出现了相同的错误:/
有没有人因为我不知道如何解决这个问题而有类似的问题
在你的索引中
export * from './components/ContextMenu';//to reexport the name exports
export {default} from './components/ContextMenu';// to reexport the default exports
不要再次导入和导出请尝试执行以下操作:-
import { default as ContextMenu } from './components/ContextMenu';
export default ContextMenu;
或
检查网页包配置的输出路径。这应该是最重要的
如果上述解决方案不起作用,则会出现问题
尝试将此添加到您的规则中:-
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
exclude: /node_modules/,
}
或
您也可以尝试两者。我认为您是在导出一个元素,而不是一个组件。请改为导出组件。但是,当事件未从上下文菜单导出时,为什么选择事件?请立即尝试更新答案。您的库是否在npmjs中?是的,它在这里:不工作我打包了我的应用程序并在本地安装了它相同的错误:/Element类型无效:需要字符串(用于内置组件)或类/函数(用于复合组件),但得到了:object。它可能与webpack有关吗?\Hi这是我的webpack配置的外观,我认为一切看起来都很好:我认为问题在于您正在使用babel loader处理TS文件,您需要使用TS loader或awesome typescript loader。如下:-是,缺失。。很棒的typescript加载器,我安装成功了你是什么意思???我该如何更改它,您看到源代码了吗:??尝试将
const ContextMenu
更改为export const ContextMenu
,并从ContextMenu.tsx和index.ts中删除export default ContextMenu
从“./components/ContentMenu”和导出默认上下文菜单
。
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},