gpt4 book ai didi

reason - 如何从 Reason 中键入 JS 模块方法?

转载 作者:行者123 更新时间:2023-12-04 07:36:31 25 4
gpt4 key购买 nike

为了将现有的基于 JS 的 WebUI 重构为 ReasonML,我试图嵌入一些重构的代码。目前,我通过将所有 ReasonML 代码(到目前为止)嵌入到 iframe 中来做到这一点。 .
空间非常有限,因此我不想添加一个具有相同功能的 reasonml 模块,而是想在我的 ReasonML 代码中使用其中一种 JS 方法,但我不清楚键入调用的语法。在 JS 中,调用很简单:

toastr.success("You're awesome");
toastr 似乎没有显式构造函数对象,但无论如何我都尝试这样做:
type bread;
[@bs.new] external toastr: unit => bread = "toastr";
[@bs.send] external success: bread => string = "success";
稍后在代码中:
let bread = toastr();
bread->success("Would be surprised if this worked.");
以上无法编译,在 bread->success("") 上失败符合以下信息:
This expression has type string
It is not a function.
如何使这项工作(在 bs 8.4.2 中)?

编辑#2
我已经更接近我希望可以使用的 JS 输出:
[@bs.scope "toastr"][@bs.val] external success: string => unit = "success";
我可以调用:
success("It worked?");
产生JS:
toastr.success("It worked?");
但它在运行时抛出异常,因为它找不到 toastr模块。
我想我现在遇到了另一个问题?

最佳答案

我相信您可以使用@module 实现您的目标:

@module("toastr") external success: string => unit = "success";
可在此处找到相关文档: https://rescript-lang.org/docs/manual/latest/import-from-export-to-js#import-a-javascript-modules-named-export

关于reason - 如何从 Reason 中键入 JS 模块方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67710323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com