【问题标题】:Using elm-reactor with Elm embedded in HTML?将榆树反应器与嵌入 HTML 的榆树一起使用?
【发布时间】:2016-12-26 16:22:39
【问题描述】:

所以我正在一起尝试 Elm 和 WebRTC。但是对于 WebRTC,我需要一些与 javascript 的互操作。因此,我为 WebRTC 和 main.js 创建了一个包含所需脚本的 index.html。

但是,我使用的是 elm-reactor。这是超级好。但是没有 main.js。我可以用 elm-make 创建它,但是我必须手动更新它。

那么,有没有办法让 elm-reactor 与嵌入式 elm 一起工作?

注意:我使用的是 Elm 0.18,截至本文撰写时是最新版本。

【问题讨论】:

    标签: elm elm-reactor


    【解决方案1】:

    你可能想看看elm-live

    它具有与elm-reactor 相同的选项,但您可以使用自己的index.html

    【讨论】:

      【解决方案2】:

      从今天开始(0.18.0),您不能使用 elm-reactor 将您的应用程序嵌入到自定义 HTML 中。如果没有额外的设置,也无法使用 elm-reactor 进行端口订阅。

      考虑使用 Create Elm Appalternatives. 之类的东西

      【讨论】:

        【解决方案3】:

        如果你愿意破解一下,完全可以use your own index.html with elm reactor。只需将以下内容放入 index.html 文件并在反应器中打开它(例如http://localhost:8000/src/index.html):

        <html>
        
        <head>
          <meta charset="UTF-8">
          <title>Title</title>
          <link rel="stylesheet" href="styles.css"><!-- Put your styles in folder with index.html -->
        </head>
        
        <body>
          <div style="width: 100%; height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; color: #9A9A9A; font-family: &#39;Source Sans Pro&#39;;">
            <div style="font-size: 3em;">Building your project!</div>
            <img src="/_reactor/waiting.gif">
            <div style="font-size: 1em">With new projects, I need a bunch of extra time to download packages.</div>
          </div>
        </body>
        
        <!-- Fonts and external JS and styles are available, I've put Ace for example -->
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.4/ace.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.4/theme-chrome.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.4/worker-lua.js"></script>
        
        <!-- Put the name of your app here (my sources place in `src` forder) -->
        <script type="text/javascript" src="/_compile/src/YourApp.elm"></script>
        
        <!-- Removes splash and starts elm app. -->
        <script type="text/javascript">
        while (document.body.firstChild) {
          document.body.removeChild(document.body.firstChild);
        }
        runElmProgram();
        </script>
        
        </html>
        

        如果你想使用端口或标志,使用the following example(你需要Elm.App.fullscreen(flags)等来使用端口,但runElmProgram()显示错误):

        <!doctype html>
        <meta charset=utf-8>
        <title>a title</title>
        <link href=/bundle.css rel=stylesheet>
        <body></body>
        <script src="/_compile/Main.elm"></script> <!-- this will fail in production -->
        <script src="/elm-bundle.js"></script> <!-- this will fail in development -->
        <script>
        var app
        var flags = {}
        
        try {
          app = Elm.App.fullscreen(flags)
        
          /* app.ports is now available */
        } catch (e) {
          // this will run in case there are compile errors:
          var stylesheets = document.querySelectorAll('link')
          for (var i = 0; i < stylesheets.length; i++) {
            stylesheets[i].parentNode.removeChild(stylesheets[i])
          }
          runElmProgram()
        }
        </script>
        

        【讨论】:

        • 有没有人用这种方法探索历史?我最终将我的应用程序的 div 放在 &lt;div&gt;class="elm-overlay"&lt;/div&gt; 前面,使其无法交互。也许这值得作为一个单独的问题提出?
        • @Charlie 有趣,我刚刚仔细检查了我的一个旧项目,它对我有用。我的猜测是你有 z-index 问题。 .elm-overlay {z-index: 100;}(或任何比你使用的更高的东西)应该会有所帮助。
        • 是的,做到了!不知道为什么会这样,但这是一个伟大而简单的解决方案。谢谢!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多