Session会话用于在用户使用应用程序时保存数据,用户离开应用程序后,该数据将被删除。

在本章中,无涯教程将学习如何设置Session会话对象,存储一些数据并返回该数据。

meteorApp.html

<head>
   <title>meteorApp</title>
</head>
 
<body>
   <div>
      {{> myTemplate}}
   </div>
</body>
 
<template name="myTemplate">
</template>

现在,无涯教程将使用 Session.set()方法在本地存储 myData ,设置方法后,无涯教程可以使用 Session.get()方法将其返回。

meteorApp.js

if (Meteor.isClient) {
  
   var myData={
      key1: "value1",
      key2: "value2"
   }

   Session.set('mySession', myData);

   var sessionDataToLog=Session.get('mySession');
   console.log(sessionDataToLog);
}

如果检查控制台,无涯教程将看到已存储的数据已记录。

Meteor Session Log

在下一章中,无涯教程将学习如何使用Session变量自动更新模板。

参考链接

https://www.learnfk.com/meteor/meteor-session.html