首页 > 解决方案 > 无法使用 IntelliJ IDEA 链接从 Kotlin/JS 项目生成的 JS 脚本

问题描述

如上所述,问题是我无法将编译后的 JS 代码从 Kotlin 项目(称为twf)链接到我的 web 项目。无论我尝试什么,Chrome 都会输出错误,例如twf.stringToExpression is not a function

就我而言,Kotlin 代码确实可以正确编译,并且输出文件的开头twf.js如下所示:

if (typeof kotlin === 'undefined') {
throw new Error("Error loading module 'twf'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'twf'.");
}var twf = function (_, Kotlin) {
'use strict';
var setOf = Kotlin.kotlin.collections.setOf_mh5how$;
...

这意味着该变量twf确实存在。此外,在同一个文件中,我发现了这个:

function stringToExpression(string, scope, isMathMl, functionConfiguration, compiledConfiguration) {
if (scope === void 0)
  ...
if (isMathMl === void 0)
  ...
if (functionConfiguration === void 0) {
  ...
}if (compiledConfiguration === void 0)
  ...

所以这个函数存在,我可以只用一个字符串作为输入来调用它,就像我在index.html文件中所做的那样(我知道在 HTML 文件中有一个裸脚本是一个坏习惯,但它只是一个原型):

<script type="text/javascript">
  ...
  var testTaskString = "a-b/c";
  console.log(twf.stringToExpression(testTaskString));
  ...
</script>

尽管如此,我仍然收到我上面提到的错误,我似乎不知道该怎么做。我该如何解决?

PS这是<head>页面的一部分(两者kotlin.jstwf.js链接):

<title>PixiJS Test</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/style.css">
<script src="lib/pixi.js"></script>
<script src="lib/kotlin.js"></script>
<script src="lib/twf.js"></script>

PPS 我什@JsName("stringToExpression")至在编译之前将 Kotlin 代码中的函数放在了函数之前,但这似乎没有帮助。

标签: javascriptkotlin

解决方案


Looks like this question was just an another instance of the "look through the code that you are using more carefully" problem, since the solution was quite trivial. When I used the console.log(twf) command (since I was sure that the twf variable was declared) I get the following output:

> Object
  > api:
    ...
    > stringToExpression: ƒ stringToExpression( ... )
    ...
  > baseoperations:
  ...

Hence, as far as I'm concered and as far as it works, I had to use the following code to refer to the function instead:

var testTaskString = "a-b/c";
console.log(twf.api.stringToExpression(testTaskString));

推荐阅读