12

我成功运行了 ReactJS.Net,包括使用 ES6 语法。

我正在使用使用 Babel 的默认 Jsx 转换管道。在浏览器中查看源代码,我可以看到 ES6 代码已被 ReactJS.Net 转换为 ES5

我一直无法让模块工作。

浏览器给了我2个错误:

Uncaught ReferenceError: exports is not defined
Uncaught ReferenceError: require is not defined

如何使用 ES6 模块?

最简单的例子:

库.js

export function square(x) {
    return x * x;
}

UserOfLib.js

import { square } from 'Lib';
console.log(square(11)); 

转换为这个(查看浏览器源代码):

库:

// @hash v3-AD133907ABEC5D32B3768A3AF2301FC9
// Automatically generated by ReactJS.NET. Do not edit, your changes will be overridden.
// Version: 2.0.1 (build 5e9476a)
// Generated at: 08-Nov-15 6:40:26 AM
///////////////////////////////////////////////////////////////////////////////
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.square = square;

function square(x) {
    return x * x;
}

用户库:

// @hash v3-812C209AFED25C2B4507E5769B0D899B
// Automatically generated by ReactJS.NET. Do not edit, your changes will be overridden.
// Version: 2.0.1 (build 5e9476a)
// Generated at: 08-Nov-15 6:40:26 AM
///////////////////////////////////////////////////////////////////////////////
var _Lib = require('Lib');

console.log((0, _Lib.square)(11)); // 121
4

1 回答 1

10

Currently, ReactJS.Net does not handle modules. If you want to use modules, you'll need to use a module bundler such as Webpack or Browserify to compile the modules down to vanilla JavaScript. Implementing support for modules in ReactJS.NET itself is a non-trivial amount of work, since it'd need to deal with dependencies and loading modules in the correct order, and good well-tested solutions like Webpack already exist.

于 2015-11-07T21:54:16.460 回答