【问题标题】:How to load es6 module into amd architecture如何将es6模块加载到amd架构中
【发布时间】:2016-07-06 15:54:14
【问题描述】:

我有一个 requirejs 应用程序,我想将一个反应模块/组件附加到我的页面中。 react 模块是用 babel / jsx 编写的。所以我使用 AMD 转换插件并将它们连接到一个文件中。

现在当然每个 react js 文件都有export default class / function-part。 其中一个反应文件有一个名为 startUp(html) 的函数,它需要一个 html 元素作为参数。这个html参数将被ReactDOM.render调用使用。

我的问题是我无法访问 AMD 模块中的启动方法:

////////////////////// REACT AND BABEL / ES6 CODE /////////////////////////
// react-module.js
export default class MyReactContainer extends React.Component {
    render() {
        return <div>Just a DIV</div>
    }
}

// react-main.js
export default function startUp(html) {
  ReactDOM.render(<MyReactContainer />, html);
}

//////////////////// EXISTING AMD MODULE /////////////////////////////////
// reactModuleIncluder.js
define(['./react-main'], function(reactMain) {

  function includeReactModule(html) {
      reactMain(html); // Here I want to call the startUp method
  }

  return includeReactModule;
});

知道我能做什么吗?或者这甚至可能吗?

编辑:解决了这个问题。文件被转译并连接到一个 js 文件中。现在我删除了 concat-process 并且它可以工作。

【问题讨论】:

  • React 代码是先用 Babel 转译的吗?乍一看,您似乎正在将 ES6 语法混合到 ES5 项目中。
  • 纯粹从混合 ES6 和 ES5 代码和 RequireJS 的观点来看,你没有理由不能这样做。如果你告诉 Babel 生成 AMD 模块,你的 ES6 代码的行为与一开始就用 ES5 编写的模块不会有什么不同。因为我不使用 React,所以不能说什么。

标签: reactjs requirejs amd jsx babeljs


【解决方案1】:

解决了这个问题。文件被转译并连接到一个 js 文件中。现在我删除了 concat-process 并且它可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-28
    • 2017-10-12
    • 2015-04-29
    • 2020-01-10
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多