【问题标题】:React app + compiled libraries > require not definedReact 应用程序 + 编译库 > 需要未定义
【发布时间】:2019-09-25 16:33:02
【问题描述】:

我正在使用已编译的库在我的 Drupal 上创建一个 React 应用程序。

    https://unpkg.com/react@16/umd/react.production.min.js: { external: true, minified: true }
    https://unpkg.com/react-dom@16/umd/react-dom.production.min.js: { external: true, minified: true }
    https://unpkg.com/babel-standalone@6.15.0/babel.min.js: { external: true, minified: true }

在自定义代码中,我定义了一个文件 (app.js),我可以看到 react 应用程序运行良好。

示例代码:

class App extends React.Component {

// Define default values.
constructor(props) {
  super(props);
  // Setting up initial state
  this.state = {
    data: []
  }
}

// Call the componentDidMount().
componentDidMount() {

// Here I build a code to create a value to render.

}

render() {
  // Here I call the element to render...
    })
  return (
  .....

但是,如果我想调用外部组件,我会遇到问题。如果我在 app.js 的开头添加导入,我会收到错误:

// import { mycomponent } from './mycomponent.js';
// import mycomponent from './mycomponent';
// const { mycomponent } from './mycomponent';
// ....

error: .... require not defined

为了调用我的组件,我尝试使用 import 或 const 但没有成功。

我不在这个项目上使用 node.js(或 browserify...),只使用上面编译的库。在阅读了很多帖子之后,我看不到如何导入/调用我的组件。

我可以在没有这些默认 React 元素的情况下调用外部组件吗:node.js...?

感谢您的帮助。

【问题讨论】:

    标签: javascript reactjs react-native drupal


    【解决方案1】:

    我不知道你设置的其他细节,但要使用import,你需要将你的代码编译成 ES2015 代码。 babel 插件是@babel/polyfill:你可以阅读更多here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-03
      • 2022-01-25
      • 2020-01-03
      • 2015-11-02
      • 2014-09-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多