我\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 文件的一部分。
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}\nRun Code Online (Sandbox Code Playgroud)\n\n有趣的是,如果我运行,完全相同的文件(Main.elm、index.html)打开得很好(按预期显示计数器)elm-live Main.elm --open -- --output=elm.js。
因此,this在 Electron 中传递给 elm.js 的内容似乎是未定义的,这导致了 scope未定义。
Chrome 开发工具显示scope传递给 elm.js 的变量是undefined在 Electron 应用程序中。对于 elm-live,该值就是Window对象。
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);\nRun 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));\nRun Code Online (Sandbox Code Playgroud)\n\n错误信息
\n\nUncaught 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\nRun 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>\nRun Code Online (Sandbox Code Playgroud)\n\nMain.elm
\n\nimport 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 ]\nRun Code Online (Sandbox Code Playgroud)\n
在 Slack 上的 Elm # Electron 频道中,我看到了有关此问题的讨论,这里为感兴趣的人提供了回顾。
scopeElm 运行时为undefined.| 归档时间: |
|
| 查看次数: |
2328 次 |
| 最近记录: |