Meteor js:使Session对象部分持久化

Pet*_*rov 6 session meteor

在我的meteor应用程序中,我使用Session来存储有关用户活动的临时信息.我想通过amplify.js将这些信息的某些部分保存到浏览器中,但不是全部.

我想要一种方法来获得'临时'会话密钥和'持久'会话密钥.我可以打电话

Session.set('persistent', 'this is persisted to browser memory');
Session.set('temporary', 'this will be erased on page reload, etc');
Run Code Online (Sandbox Code Playgroud)

然后在页面重新加载后

Session.get('persistent'); // returns 'this is persisted to browser memory'
Session.get('temporary'); // returns undefined
Run Code Online (Sandbox Code Playgroud)

在SO上发现了一个相关的帖子,但是这样可以保存这个方法,它会持续整个Session对象,我不想这样做.另外,我不想为此使用MongoDB,我希望存储只是纯粹的客户端...

提前谢谢了!

Aks*_*hat 10

使用localStorage.如果你希望它被反应,这有点棘手,但我想你可以用它Session来完成它localStorage

启动时从浏览器的localStorage jar获取项目

Meteor.startup(function() {
    Session.set("yourItem", localStorage.getItem("yourItem"));
});
Run Code Online (Sandbox Code Playgroud)

设置时:

localStorage.setItem("yourItem", "yourValue");
Session.set("yourItem", localStorage.getItem("yourItem"));
Run Code Online (Sandbox Code Playgroud)

除非你使用MongoDB或其他东西,否则一件事是不可能的,如果你在一个标签上设置它,在你刷新页面之前它不会改变其他标签.但我想Session无论如何都是这样的.


Ath*_*man 7

我成功使用该软件包u2622:persistent-session利用amplify.js本地存储能力,同时让您使用Meteor Session对象来设置和访问信息.

做完之后meteor add u2622:persistent-session,你可以继续做下面这样的事情:

  • Session.setPersistent(key, value)
  • Session.setDefaultPersistent(key, value)

u2622:persistent-session 除非您手动清除浏览器的localStorage,否则将跨选项卡维护数据并重新加载.

查看Github 页面以获取更多信息