【问题标题】:Attempted import error: 'Test1' is not exported from 'Test_components'尝试导入错误:“Test1”未从“Test_components”导出
【发布时间】:2020-07-16 17:52:42
【问题描述】:

我是 React JS 的新手,我想创建一个包含多个组件的包,我可以将其发布到 npm,以便其他团队成员可以通过安装该包来重用它们。

我面临尝试导入错误:'Test1' is notexported from 'Test_components'.error 当我尝试在我的原始项目中导入该包时。

Test1 是一个示例组件,我想在该包中添加更多类似的组件。 请帮我解决错误。

这是我的自定义包结构。

-Test_Components
 |
  ---Components
    |
     ---Test1.js
  ---index.js
  ---package.json

Test1.js

   import React from "react";

   class Test1 extends React.Component {
   render() {
   return <div> This is so Fancy!</div>;
   }
   }
   export default Test1;

index.js

    import Test1 from "./Components/Test1";
    module.exports = { Test1 };

package.json

     {
     "name": "test_components",
     "version": "1.0.10",
     "description": "Put a description here",
     "main": "index.js",
     "dependencies": {
     "react": "^15.5.4",
     "webpack": "^2.6.1"
     },
     "author": {
     "name": "xxx",
     "email": "xxx@gmail.com"
    }
    }

在实际的Project Testing.js中,导入如下。

     import React from "react";
     import { Test1 } from "test_components";
     class Testing extends React.Component {
     render() {
     return <Test1></Test1>;
     }
     }
     export default Testing ;

【问题讨论】:

  • index.js 中使用module.exports = { Test1 },您尝试过使用export { Test1 } 吗?
  • 是的,我尝试导出 {Test1},在返回组件行的 Test1.js 中出现语法错误。

标签: reactjs react-native react-redux


【解决方案1】:

导入 Test1 时必须去掉括号。

 import { Test1 } from "test_components";

替换为

import  Test1  from "test_components";

我认为你的问题是重复的,你应该混合它with this一个,我仍然没有这样做的声誉。

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2020-06-26
    • 2019-10-24
    • 2021-10-20
    • 2019-12-19
    • 2023-03-11
    相关资源
    最近更新 更多