使用 ES6 模块和 ruby on Rails 的资产管道
我正在将旧的基于函数的语法 Javascript 转换为 ES6 类。 以前的代码是这样的:
var SW;
if(SW.app === undefined) SW.app = {};
SW.app.NullLicense = (function () {
"use strict";
var NullLicense;
NullLicense = function () { };
NullLicense.prototype.handleLicense = function () {
var t1 = new SW.app.Utility();
return t1;
};
return NullLicense;
}());
现在使用 ES6 模块,代码是这样的
import {Utility} from './Utility.js'
export class NullLicense {
"use strict";
constructor(){ };
handleLicense () {
var t1 = new Utility();
return t1;
}
};
由于以前每个模块都连接到全局 SW 变量,所以我们不必担心依赖关系问题。 在 app/asset
中,我们有清单文件 application.js
。它加载代码库中的所有 js 文件。现在,由于我们在每个文件中都有 export
和 import
命令,我收到此错误,导出声明可能只出现在模块的顶层
。 我想知道解决这个问题的方法是什么。
谢谢
I am converting old functional-based syntax Javascript to ES6 classes .
Previously code was like this :
var SW;
if(SW.app === undefined) SW.app = {};
SW.app.NullLicense = (function () {
"use strict";
var NullLicense;
NullLicense = function () { };
NullLicense.prototype.handleLicense = function () {
var t1 = new SW.app.Utility();
return t1;
};
return NullLicense;
}());
Now with ES6 modules the code is like this
import {Utility} from './Utility.js'
export class NullLicense {
"use strict";
constructor(){ };
handleLicense () {
var t1 = new Utility();
return t1;
}
};
Since previously every module was concatenated to global SW variable we didn't have to worry about the dependencies issue.
In app/asset
we have our manifest file application.js
. It loads all the js files in the code base. Now since we have export
and import
commands in every file , I am getting this error that export declarations may only appear at top level of a module
.
I want to know what can be the workaround for this .
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 Webpacker gem 或 webpack。
Try using Webpacker gem or webpack.