代码之家  ›  专栏  ›  技术社区  ›  ArneHugo

米特里尔:不能用m.render重新绘制m.redraw

  •  0
  • ArneHugo  · 技术社区  · 7 年前

    我可以使用 m.mount m.redraw

    var count = 0;
    
    var Counter = {
        view: function() {
            return m('main', [
                m('h1', ('Count: ' + count))
            ])
        }
    }
    
    m.mount(document.body, Counter);
    
    window.setInterval(function () {
        count++;
        m.redraw();
    }, 200);
    <html>
    <body>
        <script src="https://unpkg.com/mithril/mithril.js"></script>
        <script src="index.js"></script>
    </body>
    </html>

    但是如果我使用 m.render

    var count = 0;
    
    var Counter = {
        view: function() {
            return m('main', [
                m('h1', ('Count: ' + count))
            ])
        }
    }
    
    m.render(document.body, m(Counter)); // <-- The only changed line
    
    window.setInterval(function () {
        count++;
        m.redraw();
    }, 200);
    <html>
    <车身>
    </车身>
    </html>

    使用时如何使mithril重画 m、 渲染 而不是 ?

    1 回复  |  直到 7 年前
        1
  •  2
  •   ArneHugo    7 年前

    如前所述 here

    请注意 m.redraw m.mount m.route m.render ,您应该使用 重新绘制。

    var count = 0;
    
    var Counter = {
        view: function() {
            return m('main', [
                m('h1', ('Count: ' + count))
            ])
        }
    }
    
    m.render(document.body, m(Counter));
    
    window.setInterval(function () {
        count++;
        m.render(document.body, m(Counter)); // <-- Use m.render here, not m.redraw
    }, 200);
    <html>
    <body>
        <script src="https://unpkg.com/mithril/mithril.js"></script>
        <script src="index.js"></script>
    </body>
    </html>