[英]Import js file to webpack application js
I have ruby project where I add typescript and webpacker 我有ruby项目,在其中添加了打字稿和webpacker
I have wrote typescript and make rake assets:tsc
. 我已经写了打字稿,并做了
rake assets:tsc
。 So it compiles ts code to js. 因此,它将ts代码编译为js。
Now I have welcome.js at folder `app/assets/javascripts/generated/welcome.js 现在,我在文件夹“ app / assets / javascripts / generation / welcome.js”中有welcome.js。
Here is code of welcome.js 这是welcome.js的代码
var HelloWorld = /** @class */ (function () {
function HelloWorld(name) {
this.name = name;
}
HelloWorld.prototype.print = function () {
alert("Hello World, " + this.name + "!");
};
return HelloWorld;
}());
new HelloWorld('John Doe').print();
//# sourceMappingURL=welcome.js.map
I need to import it to webpacker application.js
for example. 例如,我需要将其导入到webpacker
application.js
。 I cannot find any tutorial how to do this on ruby on rails. 我找不到任何教程如何在Rails上的ruby上执行此操作。
how I can solve this? 我该如何解决?
UPDATE UPDATE
I tried to wrote in welcome.js
我试图写在
welcome.js
export default HelloWorld;
And in application.js
wrote import HelloWorld from 'generated'
然后在
application.js
import HelloWorld from 'generated'
And get this in console 并在控制台中获取
Uncaught Error: Cannot find module "generated" at Object.
未捕获的错误:在对象上找不到模块“生成”。 (application.js:1) at webpack_require (bootstrap 24a076357adc3b41e79b:19) at Object.defineProperty.value (bootstrap 24a076357adc3b41e79b:62) at bootstrap 24a076357adc3b41e79b:62
(application.js:1)位于webpack_require (bootstrap 24a076357adc3b41e79b:19)位于Object.defineProperty.value(bootstrap 24a076357adc3b41e79b:62)at bootstrap 24a076357adc3b41e79b:62
Is application.js
inside /app/javascripts/packs
? application.js
是否在/app/javascripts/packs
? If so you should be able to write: 如果是这样,您应该能够写:
import { HelloWorld } from 'generated/welcome'
at the top of application.js
, then you can include it in your layout with: 在
application.js
的顶部,然后可以使用以下命令将其包括在布局中:
<%= javascript_pack_tag 'application' %>
I use this syntax in my JS classes: 我在JS类中使用以下语法:
export class HelloWorld { var HelloWorld = /** @class */ (function () { ...} }
Although you might want different class/function names. 尽管您可能想要不同的类/函数名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.