在 Angular/Ionic 项目中,我将在哪里使用以下属性来适当地处理我的需求?
document.addEventListener('pause', actiontobeperformed, false);
document.addEventListener('resume', actiontobeperformed , false);
我的需求是:
我正在构建一个受保护的应用程序,这意味着您只能在以下情况下查看内容:
您输入正确的访问代码 您的会话尚未超时 但是,当您转到“主页”时,我想以某种方式记录会话已结束,并且用户返回应用程序时需要进行身份验证。
$(document).ready(function () {
document.addEventListener('pause', actiontobeperformed, false);
document.addEventListener('resume', actiontobeperformed , false);
});
在你的 js 第一行或 html 中的 script 标签中
实现您所描述的功能的最简单方法如下:
用户使用其凭据验证其会话后,您可以使用
localStorage.login=1;
将值设置到本地存储中。
您现在添加用于暂停的事件侦听器,如
document.addEventListener('pause', actiontobeperformed, false);
,然后调用函数 actiontobeperformed
。
function actiontobeperformed() {
localStorage.login=0;
}
您唯一还需要的是检查登录状态的功能。因此你可以使用 if else 语句
if (localStorage.login == 1) {
goto menu;
} else {
goto loginpage;
}
如果您使用 Ionic,则应该使用 $ionicPlatform Service,它是 ionic.Platform 的抽象。 了解更多
$ionicPlatform.on('resume', function(){
// your staff here
});
$ionicPlatform.on('pause', function(){
// your staff here
});
因此,如果您想在用户按下 Home 按钮时关闭会话,您应该在 Pause 代码块中调用 AuthService.logout() 方法。
这正是我正在讨论的问题。我的需求是能够将 Ionic Angular 应用程序放在手机后台,然后在几个小时后恢复。如果应用程序在检查登录间隔后恢复,用户将被发送到 B2C 以检查其登录情况。
在构造函数中的app.component.ts中,
constructor(public platform: Platform){
) {
this.initializeApp();
}
async initializeApp() {
this.platform.ready().then(async () => {
/**
* The pause event emits when the native platform puts the application
* into the background, typically when the user switches to a different
* application. This event would emit when a Cordova app is put into
* the background, however, it would not fire on a standard web browser.
*/
this.platform.pause.subscribe((e) => this.pause());
this.pause();
/**
* The resume event emits when the native platform pulls the application
* out from the background. This event would emit when a Cordova app comes
* out from the background, however, it would not fire on a standard web browser.
*/
this.platform.resume.subscribe((e) => this.resume());
document.addEventListener('visibilitychange', (event) => {
if (document.visibilityState == 'visible') {
this.resume();
} else {
this.pause();
}
});
}
然后在暂停和恢复期间,您可以执行您需要应用程序执行的任何操作。