我有一个名为 Main.kt
的 Kotlin 文件
package com.ahp.mui
fun main(args: Array<String>) {
println("...")
println("Hello World")
}
项目设置为 KotlinJS 项目。
点击编译后,我看到生成了以下文件:
target/production/ahp-mui/ahp-mui.js
target/production/ahp-mui/ahp-mui.js.map
target/production/ahp-mui/ahp-mui.meta.js
主要生成的文件包含:
(function (Kotlin) {
'use strict';
var _ = Kotlin.defineRootPackage(null, /** @lends _ */ {
com: Kotlin.definePackage(null, /** @lends _.com */ {
ahp: Kotlin.definePackage(null, /** @lends _.com.ahp */ {
mui: Kotlin.definePackage(null, /** @lends _.com.ahp.mui */ {
main_kand9s$: function (args) {
Kotlin.println('...');
Kotlin.println('Hello World');
}
})
})
})
});
Kotlin.defineModule('ahp-mui', _);
_.com.ahp.mui.main_kand9s$([]);
}(Kotlin));
现在我将该文件包含在位于根目录的 index.html
中:
<html>
<head>
</head>
<body>
TESTING
<script type="text/javascript" src="target/production/ahp-mui/ahp-mui.js"></script>
</body>
</html>
现在通过 IntelliJ 直接提供 index.html
文件(http://localhost:63342/ahp-wui/ahp-mui/index.html?_ijt=n55ajlpfutv6b22n56d59jv1rn
),我看到 Kotlin
参数未定义。
ahp-mui.js:16Uncaught ReferenceError: Kotlin is not defined
我这样做正确吗?我是否应该导入任何其他包含丢失的 Kotlin
变量的内容?
最佳答案
IntelliJ 应该在某处放置一个 Kotlin 标准库 .js
文件。这是在您创建项目时配置的。您必须在编译的 .js
文件之前将其包含在 HTML 文件中。
关于javascript - KotlinJS 中的 Hello World 未定义 Kotlin 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37533768/