代码之家  ›  专栏  ›  技术社区  ›  Daniel YC Lin

清除mithril.js中的本地存储后重新加载默认值

  •  0
  • Daniel YC Lin  · 技术社区  · 6 年前

    我将这些设置保存在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 回复  |  直到 6 年前