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

刷新页面后是否可以用Knockout JS保存ViewModel的数据?

  •  0
  • UserEsp  · 技术社区  · 8 年前

    1 回复  |  直到 8 年前
        1
  •  1
  •   Akrion    8 年前

    你可以使用 ko extender 为了达到这个目的。

    外面有一些 knockout.persist

    // Will automatically load and save the value if it exists in the localStorage
    this.persistedValue = ko.observable().extend({ persist: 'persistedValue' });
    
    // Will automatically load and save the array if it exists in the localStorage
    this.persistedArray = ko.observableArray().extend({ persist: 'persistedArray' });
    

    它的整体效果非常好,因为我在一些项目中使用过它。

    现在,如果localStorage不是一个选项,那么可以通过cookie以类似的方式或通过JSON.stringify添加到浏览器中的URL。