【问题标题】:Kotlin Multiplatform: Compile Kotlin to JS, then use JS methodsKotlin Multiplatform:将 Kotlin 编译为 JS,然后使用 JS 方法
【发布时间】:2020-04-20 10:47:31
【问题描述】:

我有一个 Kotlin 多平台设置,包括 JS 和 Java 部分。在 common 文件夹中,我编写了几个应该编译为 js 的方法。如:

class test {
    init {
         println("this is completely working")
     }
}

现在,当我构建我的项目时,在 JS-exclusive 文件夹中生成了一个 node_modules 文件夹,其中包括上面的示例(尽管我希望 Kotlin 将 println 转换为 console.log 但它没有)。现在我想包含这个文件及其方法以供另一个 JS 文件读取。怎么可能做到这一点?不幸的是,关于这些多平台主题的文档并不多,我很难完成最基本的事情。

【问题讨论】:

  • 请说明您正在使用的 kotlin 版本,如果可能,也发布您的 build.gradle(.kts) 配置

标签: javascript kotlin multiplatform kotlin-multiplatform


【解决方案1】:

您需要加载由 Kotlin/JS 编译器生成的脚本。

SharedCode.js 和 BluetoothSerial.js 都是由 Kotlin/JS 编译器生成的。

<head>
    <title>WebView Callback Demo</title>
    <script src="js/kotlin.js"></script>
    <script src="js/SharedCode.js"></script>
</head>

<body>
    <div id="root">
    </div>
    <script src="js/BluetoothSerialJs.js"></script>

</body>

https://github.com/darran-kelinske-fivestars/cordova-alternative-pattern/blob/3208b24b839db6f01a27d0d5fda15cd80a4a0d12/app/src/main/assets/index.html#L4-L15

然后您可以像这样调用生成的 javascript 函数:

PackageName.Module.Function()

com.fivestars.bluetooth.BluetoothSerial.configureChannel()

这里有一个示例项目:

https://github.com/darran-kelinske-fivestars/cordova-alternative-pattern

【讨论】:

    猜你喜欢
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多