Angular 订阅未在下一个调用
Angular Subscription is not call on Next
我正在尝试实现自动登录系统,我将登录数据保存到 localStorage,以便在刷新时从本地存储中获取数据
当我从本地存储中获取数据时,我使用 Subject 来发出该数据,以便在 header 组件中我订阅数据并在刷新时保持应用程序状态
现在刷新时我的自动登录功能正在调用,但订阅没有调用 header 组件
AuthService 代码
autoLogin(){
const userdata: {
email:string;
id: string;
_token:string;
_tokenExpairDate:Date;
}=JSON.parse(localStorage.getItem('userData'));
if(!userdata){
return;
}
console.log("Auto Login Call");
const loadedData=new User
(
userdata.email,
userdata.id,
userdata._token,
new Date
(
userdata._tokenExpairDate
)
);
if(loadedData.token){
console.log(loadedData);
console.log("data Emmit");
this.user.next(loadedData);
}
else{
console.log("data Not Emmit");
}
}
header component code
ngOnInit(){
this.userSub=this.auth.user.subscribe(user=>{
console.log("Subscription Call");
this.isLogin=!user? false:true;
})
}
我不确定您实际上是如何设置 observable 的,但这可能对您有所帮助。
// In user.modal.ts export a interface for the user
// You need to define this so that you dont need to repeat your code
export interface User {
email:string;
id: string;
_token:string;
_tokenExpairDate:Date;
}
************************************
// In your service file, do this:
import { User } from './user.modal.ts';
let initialUserDetails: User = {};
private userDetailsSource: BehaviorSubject<User> = new BehaviorSubject<User>(this.initialUserDetails);
public userDetailsObservable: Observable<User> = this.userDetailsSource.asObservable();
setUserDetails(userDetails: User) {
this.userDetailsSource.next(userDetails);
}
getUserDetails(): Observable<User> {
return this.userDetailsObservable;
}
********************************
// In your component.ts file, whenever you want to set the details:
if (loadedData.token) {
console.log(loadedData);
console.log("data Emmit");
this.user.next(loadedData); // Dont do this:
this.yourServiceName.setUserDetails(loadedData); // Do this;
}
// Whenever you want to get the details of the Observable, you can do this:
this.yourServiceName.getUserDetails().subscribe(subscribedData => {
console.log('Latest Observable Data =>', subscribedData);
})
我正在尝试实现自动登录系统,我将登录数据保存到 localStorage,以便在刷新时从本地存储中获取数据 当我从本地存储中获取数据时,我使用 Subject 来发出该数据,以便在 header 组件中我订阅数据并在刷新时保持应用程序状态
现在刷新时我的自动登录功能正在调用,但订阅没有调用 header 组件
AuthService 代码
autoLogin(){
const userdata: {
email:string;
id: string;
_token:string;
_tokenExpairDate:Date;
}=JSON.parse(localStorage.getItem('userData'));
if(!userdata){
return;
}
console.log("Auto Login Call");
const loadedData=new User
(
userdata.email,
userdata.id,
userdata._token,
new Date
(
userdata._tokenExpairDate
)
);
if(loadedData.token){
console.log(loadedData);
console.log("data Emmit");
this.user.next(loadedData);
}
else{
console.log("data Not Emmit");
}
}
header component code
ngOnInit(){
this.userSub=this.auth.user.subscribe(user=>{
console.log("Subscription Call");
this.isLogin=!user? false:true;
})
}
我不确定您实际上是如何设置 observable 的,但这可能对您有所帮助。
// In user.modal.ts export a interface for the user
// You need to define this so that you dont need to repeat your code
export interface User {
email:string;
id: string;
_token:string;
_tokenExpairDate:Date;
}
************************************
// In your service file, do this:
import { User } from './user.modal.ts';
let initialUserDetails: User = {};
private userDetailsSource: BehaviorSubject<User> = new BehaviorSubject<User>(this.initialUserDetails);
public userDetailsObservable: Observable<User> = this.userDetailsSource.asObservable();
setUserDetails(userDetails: User) {
this.userDetailsSource.next(userDetails);
}
getUserDetails(): Observable<User> {
return this.userDetailsObservable;
}
********************************
// In your component.ts file, whenever you want to set the details:
if (loadedData.token) {
console.log(loadedData);
console.log("data Emmit");
this.user.next(loadedData); // Dont do this:
this.yourServiceName.setUserDetails(loadedData); // Do this;
}
// Whenever you want to get the details of the Observable, you can do this:
this.yourServiceName.getUserDetails().subscribe(subscribedData => {
console.log('Latest Observable Data =>', subscribedData);
})