【问题标题】:Calling ES 3rd party script method from F# Fable从 F# Fable 调用 ES 3rd 方脚本方法
【发布时间】:2017-08-19 07:07:28
【问题描述】:

我正在尝试让 Fable 正确编译以下代码,但无法这样做:

module AppView
#r "../../../node_modules/fable-core/Fable.Core.dll"
open Fable.Core
open Fable.Import.Browser
open Fable.Core.JsInterop
[<Import("default", from="../../../js/3rd/riot.js")>]
module riot_js =
  let mount:((string*obj)->array<obj>) = jsNative

type App
  (
    tagName:string
    ,state
    ,store
  ) = 
  member public x.AppTag =
    (riot_js?mount ("app", state))
    // does not compile: The value or constructor 'riot_js' is not defined.
    // (riot_js.mount ("app", state))
    // compiles wrongly to: riot_js.mount(["app", this.state]);

尝试riot_js?mount 会神奇地导致riot_js 不再存在,而尝试riot_js.mount 会编译成riot_js.mount(["app", this.state]);。

Mount 不接受一个参数,而是接受 2 个参数,但它要么不会转译,要么转译错误。

现在我有一个看起来最奇怪的解决方案:

[<Emit("riot_js")>]
let riot_js (x: int): obj = jsNative
...
((riot_js 1)?mount ("app", state))

这会返回一个数组,但 Fable 再次不允许我以“正常”方式获取第一个元素:

((riot_js 1)?mount ("app", state))?[0]

在[ 上给我红色,错误Unexpected symbol '[' in expression. Expected identifier, '(' or other token.

和

((riot_js 1)?mount ("app", state)).[0]

出现错误The field, constructor or member 'Item' is not defined.的所有内容都显示为红色

以下“作品”

((riot_js 1)?mount ("app", state))?``0``

并编译为:

riot_js.mount("app", this.state)["0"];

不是别人能得到的最好结果。在打开 Fable 的 2 个问题之前,我将让这个问题搁置一段时间,并设置赏金一周左右的时间。

【问题讨论】:

    标签: f# fable-f#


    【解决方案1】:

    以下似乎编译到正确的 ES 并且不需要 ? 所以它会被强类型化。

    open Fable.Core.JsInterop
    type Riotjs = 
      {
        mount:(System.Func<string,obj,string []>)
      }
    let riot = (importAll<obj> "../js/3rd/riot.js") :?> Riotjs
    let app = riot.mount.Invoke("app",(createObj []))
    

    我将初始状态设置为类型obj,但也可以使用强类型应用程序状态。

    生成的ES是:

    export var app = riot.mount("app", {});
    

    【讨论】:

      猜你喜欢
      • 2013-01-26
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      相关资源
      最近更新 更多