本文介绍了秘银:无法使用m.render重画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以控制何时重绘视图.

I have an app where I want to control when to redraw the view.

我可以使用m.mountm.redraw使其工作:

I can make it work using m.mount and 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(因为我不需要秘银来自动重绘),它将不再起作用:

But if i use m.render (because I don't need mithril to autoredraw) it no longer works:

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>
<body>
    <script src="https://unpkg.com/mithril/mithril.js"></script>
    <script src="index.js"></script>
</body>
</html>

使用m.render而不是m.mount时如何使秘银重绘?

How can I make mithril redraw when using m.render instead of m.mount?

推荐答案

如所述此处在秘银文档中:

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>

这篇关于秘银:无法使用m.render重画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:34