如何将Kotlin函数以正确的名称导出到Javascript

问题描述:

我试图将Kotlin函数导出到Javascript。问题是,这需要参数功能Kotlin2JS手术后更名,这里有一个例子:如何将Kotlin函数以正确的名称导出到Javascript

科特林来源:

fun withParam(args: String) { 
    println("JavaScript generated through Kotlin") 
} 

fun withoutParams() { 
    println("Without params") 
} 

后Kotlin2JS,试图要求在节点REPL:

> const kotlinBundle = require('./build/index.js'); 
undefined 
> kotlinBundle 
{ 'withParam_61zpoe$': [Function: withParam], 
    withoutParams: [Function: withoutParams] } 
> 

由于你可以看到,带有后缀_61zpoe$的参数功能。是否有可能摆脱那部分?

我使用kotlin2js插件和kotlin-stdlib-js:1.1.1图书馆,我kotlinOptions是:

compileKotlin2Js.kotlinOptions { 
    moduleKind = "commonjs" 
    outputFile = "build/index.js" 
} 

感谢

编译

您可以使用@JsName注释,以提供功能确切名称(或其他符号) JavaScript的。 也就是说

@JsName("withParam")  
fun withParam(args: String) { 
     println("JavaScript generated through Kotlin") 
} 
+0

完美无瑕地工作,谢谢! –