我使用 mongoDB 作为我的支持,
firebase.auth().onAuthStateChanges() 方法会在我们的应用程序中的身份验证状态更改时进行侦听。这太酷了。
但是在我的情况下没有发生同样的事情,因为我使用的支持不相似。
我将在下面分享我的代码,然后我会告诉你我试图通过 app.component.ts 中的构造函数获取用户的内容。当用户成功登录到我的应用程序时,我将获得具有唯一令牌的用户,我将其存储在 ionicStorage 中。同时我在 app.component.ts 的构造函数中调用了一个服务来从我的本地存储中获取用户。它正在工作但是当用户注册时我的 app.component 没有触发?我需要手动重新加载页面一次,然后触发根组件,调用服务,获取显示在侧菜单中的用户数据?
我不想重新加载我的页面来显示用户。
//app.component.ts
export class MyApp {
constructor(getDataService:GetDataService
,public storage: Storage) {
this.storage.get('user').then(userResp => {
this.user = userResp;
console.log('signup user : ' + JSON.stringify(userResp));
});
})}
//app.html
<ion-content>
<div *ngIf="user" padding>
Hi {{user.displayName}}
</div></ion-content>
谢谢,
但是当用户注册时我的 app.component 没有触发?我是否需要手动重新加载页面,然后触发根组件,调用服务,获取显示在侧菜单中的用户数据?
是的,您需要告诉应用程序组件数据已更改。但是您可以使用事件来做到这一点。因此,在您的身份验证服务中,您可以在用户登录或退出时发布一些事件:
import { Events } from 'ionic-angular';
// ...
@Injectable()
export class AccountService {
constructor(..., public events: Events) {}
public login(): any {
// your logic...
// Publish the `user:login` event, sending true
this.events.publish('user:login', true);
}
public logout(): any {
// your logic...
// Publish the `user:login` event, sending false
this.events.publish('user:login', false);
}
}
然后在你的 app.component 文件中,订阅这些事件来处理每个场景:
export class MyApp {
constructor(getDataService: GetDataService, public storage: Storage, public events: Events) {
this.storage.get('user').then(userResp => {
this.user = userResp;
console.log('signup user : ' + JSON.stringify(userResp));
});
events.subscribe('user:login', (loggedIn) => {
if(loggedIn) {
// Get the user details again
this.storage.get('user').then(userResp => {
this.user = userResp;
console.log('signup user : ' + JSON.stringify(userResp));
});
} else {
// Reset the user details shown in the side menu
this.user = null;
}
});
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句