javascript - 无法使用 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.js
都twf.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 代码中的函数放在了函数之前,但这似乎没有帮助。
解决方案
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));
推荐阅读
- django - Django jsonfield,是否可以使用json数组值长度进行过滤?
- python - 错误:找不到满足 pyautogui 要求的版本(来自版本:无)
- electron - 有没有办法在 Electron 应用程序包根目录中复制或创建文件夹?
- java - Springdoc 将安全性指定为打开
- java - 如何使用 bufferedreader 捕获 csv 文件中的字段值
- javascript - 如何使用现代 FileApi 在本地 fs 中创建文件?
- xamarin - Xamarin 在 Andriod 设备上使用黑色背景和白色二维码形成 Zxing 扫描问题
- linux - 根据命令行中同一行中的单词的条件更改另一个单词?
- c# - C#:Random.NextDouble 方法停止应用程序
- locking - 在 Marklogic 中的指定时间释放所有文档锁