I have an Ionic3/Angular4 app, the data is source depending on current logged user, so I have to wait the user data event to be fired to setup the data connection. (I use firebase to manage the user auth).
In the main component I have this:
async ngOnInit() {
this.afAuth.authState.subscribe(async data => {
if (data && !data.isAnonymous) {
await this.userService.initSettingsAsync(data.uid);
}
else {
this.nav.setRoot('LoginPage');
}
});
}
My page is this:
@IonicPage({
segment: 'customers/:customerId',
defaultHistory: ['CustomersPage'],
})
@Component({
selector: 'page-customer-detail',
templateUrl: 'customer-detail.html',
})
export class CustomerDetailPage {
customerId: string;
customer: Customer;
constructor(public navParams: NavParams, private customerService: CustomerService) {
this.customerId = navParams.get("customerId");
}
async ionViewDidLoad() {
console.log('ionViewDidLoad CustomerDetailPage');
this.customer = await this.customerService.get(this.customerId);
}
}
this.customerService.get(this.customerId)
MUST execute AFTER this.userService.initSettingsAsync(data.uid)
promise get resolved, otherwise userService doesn't know where to get the datas.
When the page is loaded by deeplink it get initialized BEFORE the authState event fire.
How can I handle this in a smart way? I mean, I don't like to put the user stuff on every page that needs data.