我正在学习ElectronJs所以我开始用它制作一个简单的AngularJs应用程序,在这里我开始磕磕绊绊。
我想存储一些持久性数据,如用户令牌和电子邮件ID等。所以,为了做到这一点,我使用的是$cookieStore
。
下面给出的示例在(firefox,chrome)浏览器中工作正常,但是当我在电子浏览器窗口中运行相同的示例时,它会暂时存储值并在重新启动时丢失值。 / p>
示例:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular-cookies.min.js"></script>
<meta charset="utf-8">
<title>AngularJS Cookies Example</title>
</head>
<body ng-controller="MyController">
{{platformCookie}} {{myFruit}} {{myFlower}}
<input type="button" value="setCookie" ng-click="setCookie()" />
<input type="button" value="getCookie" ng-click="getCookie()" />
<script>
var myApp = angular.module('myApp', ['ngCookies']);
myApp.controller('MyController', ['$scope', '$cookies', '$cookieStore',
function($scope, $cookies, $cookieStore) {
$cookies.userName = 'Vikas';
$scope.setCookie = function()
{
$scope.platformCookie = $cookies.userName;
$cookieStore.put('fruit', 'Apple');
$cookieStore.put('flower', 'Rose');
console.log('set cookies');
}
$scope.getCookie = function()
{
$scope.myFruit = $cookieStore.get('fruit');
$scope.myFlower = $cookieStore.get('flower');
console.log($scope.myFruit);
}
}]);
</script>
</body>
</html>
我还尝试使用Electron Session来创建持久性cookie,但是,它也无法正常运行并在应用程序重启时被处理掉。
代码:
const session = require('electron').session;
const ses = session.fromPartition('persist:name');
// Query all cookies associated with a specific URL.
ses.cookies.get({ url: 'http://www.github.com' }, (error, cookies) => {
console.log(cookies);
});
// Set a cookie with the given cookie data;
// may overwrite equivalent cookies if they exist.
const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' };
ses.cookies.set(cookie, (error) => {
if (error)
console.error(error);
});
在第二次运行应用程序时使用此代码,它应该显示已设置但没有运气的cookie数据。
答案 0 :(得分:2)
如果您不给expirationDate
let cookie = {
url: 'http://www.drutas.com',
name: 'userDetailsGoogle',
value: JSON.stringify(userDetailsGoogle),
expirationDate: 2093792393999
};
如果你给他们expirationDate
,那么cookie将继续存在