在 html 脚本标签中使用 es6 类
Using es6 classes in html script tag
我想开始在 ES6 中编写我的 JS 代码 类。
我正在制作模块并使用 webpack 2
将它们转录为 es5。
但是如何在 HTML 中使用它们??
<script>
var test=new Test();//Test not defined
</script>
我错过了什么?
如果你的代码已经有一半是在 ES6 中编写的,为什么还要尝试在脚本标签中编写?只需在另一个模块的 script
标记中编写您要编写的代码。
见https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact&code=class%20Greeter%20%7B%0D%0A%20%20%20%20greeting%3B%0D%0A%20%20%20%20constructor(message)%20%7B%0D%0A%20%20%20%20%20%20%20%20this.greeting%20%3D%20message%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20greet()%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20%22Hello%2C%20%22%20%2B%20this.greeting%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Alet%20greeter%20%3D%20new%20Greeter(%22world%22)%3B%0D%0A%0D%0Alet%20button%20%3D%20document.createElement('button')%3B%0D%0Abutton.textContent%20%3D%20%22Say%20Hello%22%3B%0D%0Abutton.onclick %20%3D%20function()%20%7B%0D%0A%20%20%20%20alert(greeter.greet())%3B%0D%0A%7D%0D%0A%0D%0Adocument.body.appendChild(按钮)%3B
我想开始在 ES6 中编写我的 JS 代码 类。
我正在制作模块并使用 webpack 2
将它们转录为 es5。
但是如何在 HTML 中使用它们??
<script>
var test=new Test();//Test not defined
</script>
我错过了什么?
如果你的代码已经有一半是在 ES6 中编写的,为什么还要尝试在脚本标签中编写?只需在另一个模块的 script
标记中编写您要编写的代码。
见https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact&code=class%20Greeter%20%7B%0D%0A%20%20%20%20greeting%3B%0D%0A%20%20%20%20constructor(message)%20%7B%0D%0A%20%20%20%20%20%20%20%20this.greeting%20%3D%20message%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20greet()%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20%22Hello%2C%20%22%20%2B%20this.greeting%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Alet%20greeter%20%3D%20new%20Greeter(%22world%22)%3B%0D%0A%0D%0Alet%20button%20%3D%20document.createElement('button')%3B%0D%0Abutton.textContent%20%3D%20%22Say%20Hello%22%3B%0D%0Abutton.onclick %20%3D%20function()%20%7B%0D%0A%20%20%20%20alert(greeter.greet())%3B%0D%0A%7D%0D%0A%0D%0Adocument.body.appendChild(按钮)%3B