清除mithril.js中的localstorage后重新加载默认值

时间:2019-05-18 00:57:00

标签: local-storage mithril.js

我将设置保留在localStorage中,但是如果我希望将其重置为“默认”值,如何实现“重置”按钮。

<!doctype html><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="//unpkg.com/mithril@next/mithril.min.js"></script>
</head>
<body>
<script>
var state = 'default'
var ConfigPage = {
  view: function(vnode) {
    return m('',
      m('input', {
    value: state,
    onchange: function(e) {
      state = e.target.value
      if (localStorage !== undefined) {
        var s = JSON.stringify(state)
        localStorage.setItem('t1', s)
      }
    }
      }),
      m('button',{onclick: function() {
    localStorage.clear();
      }}, "reset")
    )
  }
}
m.mount(document.body, {
  oninit: function(vnode) {
    if (localStorage !== undefined) {
      var v = localStorage.getItem("t1")
      if (v !== null) {
        state = JSON.parse(v)
      }
    }
  },
  view: function() {
    return m(ConfigPage)
  }
})
</script>
</body></html>

0 个答案:

没有答案