从node_modules导入react中的自定义组件

时间:2019-09-06 08:04:23

标签: javascript reactjs github babeljs

我正在尝试开发一个可在不同应用程序之间共享的react组件。这个想法是任何应用程序都可以加入组件,传递一些道具并在各处获得相同的功能。因此,应用程序可以将我的组件作为依赖项添加到package.json中,该依赖项将在node_modules中下载。基本上不这样做

import testComponent from "./Components";

对此

import testComponent from "my-custom-components";

我尝试做npm install [githun-url-link],看起来效果不错。但是启动应用后,我得到了错误:

Failed to Compile
./node_modules/.../src/components/testComponent.js
SyntaxError: /Users/mzq/Workspace/.../testing/node_modules/.../src/components/testComponent.js: Support for the experimental syntax 'classProperties' isn't currently enabled (7:9):

   5 | 
   6 | class testComponent extends Component {
>  7 |   state = {
     |         ^
   8 |     currentPageIndex: 0,
   9 |     currentPageLabel: this.props.currentPageLabel,
  10 |     page: this.props.pages[0],

Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.

我在进行谷歌搜索后尝试的一种解决方案是

1)yarn add @babel/plugin-proposal-class-properties --dev

2)使用以下配置在根文件夹中创建babel.config.js

module.exports = {
    presets: [
        '@babel/preset-env',
        '@babel/preset-react',
        '@babel/preset-typescript'
    ],
    plugins: [
        '@babel/plugin-proposal-class-properties'
    ]
};

无济于事。

0 个答案:

没有答案
相关问题