Electron 无法读取属性';榆树';在电子中使用Elm 0.19时的未定义
我正在用Electron测试Elm 0.19,在Electron中构建一个简单的Elm应用程序(基于反例)。当我运行Electron 无法读取属性';榆树';在电子中使用Elm 0.19时的未定义,electron,elm,Electron,Elm,我正在用Electron测试Elm 0.19,在Electron中构建一个简单的Elm应用程序(基于反例)。当我运行electron forge start时,我得到一个错误,该错误表示无法读取未定义的属性“Elm”突出显示scope['Elm']Elm.js文件的一部分 function _Platform_export(exports) { scope[‘Elm’] ? _Platform_mergeExportsDebug(‘Elm’, scope[‘Elm’], exports)
electron forge start
时,我得到一个错误,该错误表示无法读取未定义的属性“Elm”
突出显示scope['Elm']
Elm.js文件的一部分
function _Platform_export(exports) {
scope[‘Elm’] ? _Platform_mergeExportsDebug(‘Elm’, scope[‘Elm’], exports) : scope[‘Elm’] = exports;
}
有趣的是,如果我运行elm live Main.elm--open--output=elm.js
,则完全相同的文件(Main.elm,index.html)可以正常打开(按预期显示计数器)
因此,似乎在Electron中传递给elm.js的这个
未定义,这导致范围
未定义
Chrome开发工具显示,对于Electron应用程序,传递给elm.js的范围
变量是未定义的
。对于elm live,该值是窗口
对象
elm.js
(function(scope){
'use strict';
--- omitted ----
var author$project$Main$main = elm$browser$Browser$sandbox({ init: author$project$Main$init, update: author$project$Main$update, view: author$project$Main$view });
_Platform_export({ 'Main': { 'init': author$project$Main$main(elm$json$Json$Decode$succeed(_Utils_Tuple0))(0) } });
})(undefined);
elm.js?[sm]
(function(scope){
'use strict';
--- omitted ----
var author$project$Main$main = elm$browser$Browser$sandbox(
{init: author$project$Main$init, update: author$project$Main$update, view: author$project$Main$view});
_Platform_export({'Main':{'init':author$project$Main$main(
elm$json$Json$Decode$succeed(_Utils_Tuple0))(0)}});}(this));
错误消息
Uncaught TypeError: Cannot read property 'Elm' of undefined
at _Platform_export (elm.js:1949)
at elm.js:4004
at elm.js:4005
index.html:44 Uncaught ReferenceError: Elm is not defined
at index.html:44
Index.html
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="elm"></div>
</body>
<script src="./elm.js"></script>
<script>
var app = Elm.Main.init({
node: document.getElementById('elm')
});
</script>
</html>
在Slack上的Elm#electron channel中,我看到了关于这个问题的讨论,下面是对感兴趣的人的回顾
范围未定义
- 使用以下位置的说明来修改编译的elm代码:
- 或者在编译的elm.js文件的底部将(this)更改为(this | | window)(在这里打开了一个问题)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
main =
Browser.sandbox { init = init, update = update, view = view }
-- MODEL
type alias Model = Int
init : Model
init =
0
-- UPDATE
type Msg = Increment | Decrement
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
-- VIEW
view : Model -> Html Msg
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (String.fromInt model) ]
, button [ onClick Increment ] [ text "+" ]
]