我正在使用Ionic 4 / Angular 7构建应用程序。我的app.component构造函数中有一个Storage Promise。硬重载应用程序(在浏览器中)时,它可以正常工作,但是当应用程序在更改中实时重载时,在呈现“清单”页面之前,无法解析构造函数中的promise,然后失败,因为用户不是设置好了。
app.component.ts
import { Component } from '@angular/core';
import { Platform} from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { Router } from "@angular/router";
import { Storage } from '@ionic/storage';
import { UserService } from "./services/user.service";
@Component({
selector: 'app-root',
templateUrl: 'app.component.html'
})
export class AppComponent {
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private router: Router,
private storage: Storage,
private userService: UserService,
) {
this.storage.get('user').then((user) => {
if(user) {
this.userService.user = user;
this.router.navigateByUrl('checklist');
} else {
this.router.navigateByUrl('login');
}
});
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
}
checklist.page.ts
import { Component, OnInit } from '@angular/core';
import {UserService} from "../services/user.service";
import {User} from "../models/user";
@Component({
selector: 'app-checklist',
templateUrl: './checklist.page.html',
styleUrls: ['./checklist.page.scss'],
})
export class ChecklistPage implements OnInit {
user: User;
constructor(
private userService: UserService,
) {
this.user = this.userService.user;
}
ngOnInit() {
console.log(this.user);
}
}