Kotlin Multiplatform:将Kotlin编译为JS,然后使用JS方法

问题描述 投票:0回答:1

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

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

现在,当我构建我的项目时,在JS专有文件夹中会生成一个node_modules文件夹,其中包含上面的示例(尽管我希望Kotlin将println转换为console.log,但没有)。现在,我想包括此文件及其方法,以供另一个JS文件读取。那怎么办?不幸的是,关于这些多平台主题的文档很多,因此我很难完成最基本的工作。

javascript kotlin multiplatform kotlin-multiplatform
1个回答
0
投票

您需要加载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

© www.soinside.com 2019 - 2024. All rights reserved.