Невозможно прочитать свойство Elm неопределенного при использовании Elm 0.19 в Electron - PullRequest
0 голосов
/ 20 октября 2018

Я экспериментирую с Elm 0.19 и создаю в Electron простое приложение Elm (на примере Counter).Когда я запускаю electron-forge start, я получаю сообщение об ошибке, в котором Cannot read property 'Elm' of undefined выделяет scope[‘Elm’] часть файла elm.js.

function _Platform_export(exports) {
    scope[‘Elm’] ? _Platform_mergeExportsDebug(‘Elm’, scope[‘Elm’], exports) : scope[‘Elm’] = exports;
}

Интересно то, что точно такие же файлы (Main.elm, index.html) открывается нормально (показывает счетчик, как и ожидалось), если я вместо этого запускаю elm-live Main.elm --open -- --output=elm.js.

Так что получается, что this, переданный в elm.js в Electron, не определен, что затемвызвал scope быть неопределенным.

Chrome Dev Tool показывает, что переменная scope, переданная в elm.js, равна undefined в случае приложения Electron.В случае elm-live этим значением был объект Window.

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?[см]

(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>

Main.elm

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 "+" ]
    ]

1 Ответ

0 голосов
/ 21 октября 2018

В канале Elm #electron на Slack я видел обсуждение этой проблемы, и вот резюме для всех, кто интересуется.

  1. Проблема в том, что «это» не определено в электронеrenderer
  2. Это приводит к тому, что scope в среде исполнения Elm будет undefined.
  3. Возможный обходной путь:
...