1

我正在尝试将端口与 elm-app 一起使用。以前我使用elm-live香草设置,并且能够插入这样的端口:

索引.html

<body>
    <noscript>
        You need to enable JavaScript to run this app.
    </noscript>
    <div id="root"></div>
    <script>
      window.addEventListener("load", function(event) {

        var app = Elm.Main.fullscreen(localStorage.session || null);

        app.ports.storeSession.subscribe(function(session) {
          localStorage.session = session;
        });
        ...

这行得通,elm-live 似乎嵌入elm.js<head>index.html 中。

但是,当我尝试将此设置用于带有 的端口时create-elm-app,已编译的 javascript 嵌入在 的底部<body>,因此像我所做的那样添加会<script>导致:

(index):68 Uncaught ReferenceError: Elm is not defined
    at (index):68

嵌入 JS 端口的最佳方式是什么?

4

1 回答 1

2

halfzebra/create-elm-app项目的设置略有不同。您必须修改src/index.js文件,如Javascript 互操作文档中的示例所示

import './main.css';
import { Main } from './Main.elm';
import registerServiceWorker from './registerServiceWorker';

var app = Main.embed(document.getElementById('root'));

registerServiceWorker();

// ports related code
app.ports.windowTitle.subscribe(function(newTitle){
    window.document.title = newTitle;
});
于 2018-03-25T01:40:35.250 回答