简体   繁体   English

从 kotlin 访问 js 变量

[英]Accessing js variable from kotlin

I have two files: a.js and b.kt.我有两个文件:a.js 和 b.kt。

In a.js I have a variable在 a.js 我有一个变量

var x = 'something'

Then I need to access this variable in my kotlin file, like然后我需要在我的 kotlin 文件中访问这个变量,比如

var y = x

y.doStuff()

I tried var y = String.asDynamic().x and external but it didn't work.我尝试var y = String.asDynamic().xexternal但它没有用。

So how can I get this variable in kotlin file?那么如何在 kotlin 文件中获取此变量?

You should convert window (the JS global scope object) to a dynamic and then access your property on that.您应该将window (JS 全局 scope 对象)转换为dynamic ,然后访问您的属性。

var thing = "hello";
fun main() {
    console.log(window.asDynamic().thing.unsafeCast<String>())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM