【发布时间】:2019-04-27 17:03:18
【问题描述】:
我想在 ReactJS + Typescript 环境中使用 IIFE。所以我用 ReactJS + Typescript 重写了这段代码
IIFE
(function() {
//Constructor
this.Radio = function(){
var defaults = {
locale: "en-US",
}
}
// Public Methods
Radio.prototype.load = function() {
}
}());
ReactJS + Typescript
export default class Radio{
options: any = {
locale: "en-US",
};
constructor(props: any) {
var defaults = {
locale: "en-US",
}
}
// Public Methods
load = () => {
}
}
我试图实现的是,当 bundle.js 从 html 页面加载时,我可以通过传递一些值来实例化,如下所示
<script src="main.js"></script>
<body>
<div id="app"></div>
</body>
<script>
$(function() {
var test = new Radio({ <----error on this line
locale: "en-US",
});
test.load();
});
</script>
但是当我运行上面的代码时,我得到了这个错误Uncaught ReferenceError: Radio is not defined
可以像上面那样实现 IIFE 吗?
谢谢
【问题讨论】:
标签: reactjs typescript iife