秘银:无法使用m.render重画 [英] Mithril: cannot m.redraw with m.render

查看:64
本文介绍了秘银:无法使用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?

推荐答案

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

请注意,m.redraw仅在使用m.mountm.route时才有效.如果通过m.render渲染,则应使用m.render重绘.

Note that m.redraw only works if you used m.mount or m.route. If you rendered via m.render, you should use m.render to redraw.

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重画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆