在 Electron 中使用 Elm 0.19 时无法读取未定义的属性“Elm”

Jas*_* O. 2 elm electron

我\xe2\x80\x99m 使用 Electron 试验 Elm 0.19,在 Electron 中构建一个简单的 Elm 应用程序(基于 Counter 示例)。当我运行时electron-forge start,出现错误,提示Cannot read property \'Elm\' of undefined突出显示scope[\xe2\x80\x98Elm\xe2\x80\x99] elm.js 文件的一部分。

\n\n
function _Platform_export(exports) {\n    scope[\xe2\x80\x98Elm\xe2\x80\x99] ? _Platform_mergeExportsDebug(\xe2\x80\x98Elm\xe2\x80\x99, scope[\xe2\x80\x98Elm\xe2\x80\x99], exports) : scope[\xe2\x80\x98Elm\xe2\x80\x99] = exports;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

有趣的是,如果我运行,完全相同的文件(Main.elm、index.html)打开得很好(按预期显示计数器)elm-live Main.elm --open -- --output=elm.js

\n\n

因此,this在 Electron 中传递给 elm.js 的内容似乎是未定义的,这导致了 scope未定义。

\n\n

Chrome 开发工具显示scope传递给 elm.js 的变量是undefined在 Electron 应用程序中。对于 elm-live,该值就是Window对象。

\n\n

elm.js

\n\n
(function(scope){\n\'use strict\';\n\n --- omitted ----\n\n    var author$project$Main$main = elm$browser$Browser$sandbox({ init: author$project$Main$init, update: author$project$Main$update, view: author$project$Main$view });\n    _Platform_export({ \'Main\': { \'init\': author$project$Main$main(elm$json$Json$Decode$succeed(_Utils_Tuple0))(0) } });\n})(undefined);\n
Run Code Online (Sandbox Code Playgroud)\n\n

榆树.js?[SM]

\n\n
(function(scope){\n\'use strict\';\n\n --- omitted ----\n\nvar author$project$Main$main = elm$browser$Browser$sandbox(\n    {init: author$project$Main$init, update: author$project$Main$update, view: author$project$Main$view});\n_Platform_export({\'Main\':{\'init\':author$project$Main$main(\n    elm$json$Json$Decode$succeed(_Utils_Tuple0))(0)}});}(this));\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误信息

\n\n
Uncaught TypeError: Cannot read property \'Elm\' of undefined\n    at _Platform_export (elm.js:1949)\n    at elm.js:4004\n    at elm.js:4005\nindex.html:44 Uncaught ReferenceError: Elm is not defined\n    at index.html:44\n
Run Code Online (Sandbox Code Playgroud)\n\n

索引.html

\n\n
<!DOCTYPE HTML>\n<html>\n\n<head>\n</head>\n\n<body>\n    <div id="elm"></div>\n</body>\n<script src="./elm.js"></script>\n<script>\n    var app = Elm.Main.init({\n        node: document.getElementById(\'elm\')\n    });\n</script>\n</html>\n
Run Code Online (Sandbox Code Playgroud)\n\n

Main.elm

\n\n
import Browser\nimport Html exposing (Html, button, div, text)\nimport Html.Events exposing (onClick)\n\n\nmain =\n  Browser.sandbox { init = init, update = update, view = view }\n\n\n-- MODEL\n\ntype alias Model = Int\n\ninit : Model\ninit =\n  0\n\n\n-- UPDATE\n\ntype Msg = Increment | Decrement\n\nupdate : Msg -> Model -> Model\nupdate msg model =\n  case msg of\n    Increment ->\n      model + 1\n\n    Decrement ->\n      model - 1\n\n\n-- VIEW\n\nview : Model -> Html Msg\nview model =\n  div []\n    [ button [ onClick Decrement ] [ text "-" ]\n    , div [] [ text (String.fromInt model) ]\n    , button [ onClick Increment ] [ text "+" ]\n    ]\n
Run Code Online (Sandbox Code Playgroud)\n

Jas*_* O. 5

在 Slack 上的 Elm # Electron 频道中,我看到了有关此问题的讨论,这里为感兴趣的人提供了回顾。

  1. 问题是电子渲染器中没有定义“this”
  2. 这会导致scopeElm 运行时为undefined.
  3. 可能的解决方法是