离子 4 使用 href 丢失会话并切换回应用程序
ionic 4 loss of session using href and switching back to app
我构建了一个简单的页面,它呈现了一些项目的列表。这些项目有一个 href 元素可以在浏览器中打开它。问题在于,当用户回来时,所有用于保存局部变量的 class 都会重新初始化并破坏应用程序功能。处理这个问题的正确方法是什么。我想保留数据集。下面是我的列表代码和服务中的变量 class
<ion-content>
<div class="full-screen-bg">
<div *ngFor="let alb of core.albums">
<ion-row><ion-col no-padding text-center><a href="{{alb.link}}"><img src="{{alb.image}}"></a></ion-col></ion-row>
<ion-row><ion-col class="sg-title-text">{{alb.name}}</ion-col></ion-row>
</div>
具有变量并被重置的服务 class 是
@Injectable({
providedIn: 'root'
})
export class CoreService {
loggedIn:boolean
name:string
constructor(public admob: Admob,
public sgSvc:DataServiceService) {
this.loggedIn = false
console.log("album constructor called:::" + this.loggedIn + " name:" + this.name)
}
}
简单的方法是通过 StorageService 保存登录用户(用户名、token 和过期时间),如果 token 是 none 或已经过度扩展,那么对于你的 CoreService 应该尝试从 StorageService 获取它并确认它时间意味着用户应该再次登录。
export class CoreService {
//...
public isAdmin = false;
private token = false;
public userSubject: Subject<Object> = new Subject<Object>();
//...
constructor(/*...*/) {
this.userSubject.subscribe(user => {
if (user && user['role']) {
if (user['role'] == 'admin') {
this.isAdmin = true;
} else {
this.isAdmin = false;
}
}
});
this.getLocal('user').then(val => {
this.userSubject.next(val);
});
}
//...
}
//And for other component also subscribe same subject like
export class AppComponent /*...*/ {
constructor(coreService: CoreService /*...*/) {
this.coreService.userSubject.subscribe(user => {
// your logic there
});
}
}
我构建了一个简单的页面,它呈现了一些项目的列表。这些项目有一个 href 元素可以在浏览器中打开它。问题在于,当用户回来时,所有用于保存局部变量的 class 都会重新初始化并破坏应用程序功能。处理这个问题的正确方法是什么。我想保留数据集。下面是我的列表代码和服务中的变量 class
<ion-content>
<div class="full-screen-bg">
<div *ngFor="let alb of core.albums">
<ion-row><ion-col no-padding text-center><a href="{{alb.link}}"><img src="{{alb.image}}"></a></ion-col></ion-row>
<ion-row><ion-col class="sg-title-text">{{alb.name}}</ion-col></ion-row>
</div>
具有变量并被重置的服务 class 是
@Injectable({
providedIn: 'root'
})
export class CoreService {
loggedIn:boolean
name:string
constructor(public admob: Admob,
public sgSvc:DataServiceService) {
this.loggedIn = false
console.log("album constructor called:::" + this.loggedIn + " name:" + this.name)
}
}
简单的方法是通过 StorageService 保存登录用户(用户名、token 和过期时间),如果 token 是 none 或已经过度扩展,那么对于你的 CoreService 应该尝试从 StorageService 获取它并确认它时间意味着用户应该再次登录。
export class CoreService {
//...
public isAdmin = false;
private token = false;
public userSubject: Subject<Object> = new Subject<Object>();
//...
constructor(/*...*/) {
this.userSubject.subscribe(user => {
if (user && user['role']) {
if (user['role'] == 'admin') {
this.isAdmin = true;
} else {
this.isAdmin = false;
}
}
});
this.getLocal('user').then(val => {
this.userSubject.next(val);
});
}
//...
}
//And for other component also subscribe same subject like
export class AppComponent /*...*/ {
constructor(coreService: CoreService /*...*/) {
this.coreService.userSubject.subscribe(user => {
// your logic there
});
}
}