ktor 编译成 Js 后缺少 ktor-ktor-http 库


#1

我发现我编译后的 JS 代码里 require('ktor-ktor-http')

image

可是我在 npm 仓库里却搜索不到这个库

这是我的 gradle 配置。

kotlin {
    jvm()
    js {
        compilations.main.kotlinOptions.moduleKind = 'umd'
    }
    // For ARM, should be changed to iosArm32 or iosArm64
    // For Linux, should be changed to e.g. linuxX64
    // For MacOS, should be changed to e.g. macosX64
    // For Windows, should be changed to e.g. mingwX64
//    macosX64("macos")
    iosX64("ios") {
        binaries {
            framework()
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation ("io.ktor:ktor-client-core:$ktor_version")
                implementation kotlin('stdlib-common')
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        jvmMain {
            dependencies {
                implementation ("io.ktor:ktor-client-android:$ktor_version")
                implementation kotlin('stdlib-jdk8')
            }
        }
        jvmTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        jsMain {
            dependencies {
                implementation kotlin('stdlib-js')
                implementation "io.ktor:ktor-client-js:$ktor_version"
            }
        }
        jsTest {
            dependencies {
                implementation kotlin('test-js')
            }
        }
        iosMain {
            dependencies {
                implementation ("io.ktor:ktor-client-ios:$ktor_version")
            }
        }
        iosTest {
        }
    }
}

这是我的 kotlin 代码


class LoginApi {
    private val client = HttpClient()

    fun login(param: LoginReq, callback: (String) -> Unit) {
        GlobalScope.apply {
            launch(ApplicationDispatcher) {
                val result: String = client.post {
                    url("http://10.76.34.177:3000/user/login")
                    parameter("userName", param.userName)
                    parameter("passWord", param.passWord)
                }

                callback(result)
            }
        }
    }
}

我是新手,请多指教,感谢大家。


#2

朋友,建议你把你的完整demo放github,这样大家有空了可以比较方便的帮你看下哈。


京ICP备16022265号-2 Kotlin China 2017 - 2018
本站由腾讯云提供计算服务