我按照本教程创建了一个 kotlin->js 项目:https ://kotlinlang.org/docs/tutorials/javascript/getting-started-gradle/getting-started-with-gradle.html
接下来,我按照这些说明在我的代码中使用协程:https ://github.com/kotlin/kotlinx.coroutines/blob/master/README.md#using-in-your-projects
到目前为止一切都很好,代码中没有标记错误,我可以构建我的 js 应用程序而没有任何错误消息。但是,我的 js 脚本没有在浏览器中运行,我在浏览器控制台中收到上述错误消息。你们中的任何人都知道我错过了什么或可能配置错误吗?
这是我的 build.gradle
group 'de.berlin'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.3.31'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0-M1"
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.0-M1")
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.3.0-M1"
}
compileKotlin2Js.kotlinOptions.sourceMap = true
compileKotlin2Js.kotlinOptions.outputFile = "${projectDir}/web/js/myApp.js"
compileKotlin2Js.kotlinOptions.suppressWarnings = false
compileKotlin2Js.kotlinOptions.verbose = true
build.doLast {
configurations.compile.each { File file ->
copy {
includeEmptyDirs = false
from zipTree(file.absolutePath)
into "${projectDir}/web/js/lib"
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
}
}
}
copy {
includeEmptyDirs = false
from "${buildDir}/resources/main"
into "${projectDir}/web"
}
}
clean.doFirst {
delete "${projectDir}/web"
}
一切都编译没有任何错误消息,但我在浏览器控制台中收到以下错误消息:“”未找到它的依赖项 'kotlinx-coroutines-core'。请检查是否在 '(projectname)' 之前加载了 'kotlinx-coroutines-core'。”
更详细的检查表明 /web/js/lib 仅包含 kotlin.js,不应该 kotlinx-coroutines-core 也存在,因为它是依赖块的一部分并且应该在 build.doLast-step 中复制?我还注意到编译后的 js 文件包含以下内容:
if (typeof kotlin === 'undefined') {
throw new Error("Error loading module 'myApp'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'myApp'.");
}
if (typeof this['kotlinx-coroutines-core'] === 'undefined') {
throw new Error("Error loading module 'myApp'. Its dependency 'kotlinx-coroutines-core' was not found. Please, check whether 'kotlinx-coroutines-core' is loaded prior to 'myApp'.");
}
为什么是 this['kotlinx-coroutines-core'] 而不是 kotlinx-coroutines-core (如上一行)?