我使用的是ionic 5.0.0,必须为我的登录屏幕实现加载。为此,我使用LoadingController完成了一些实现。但不幸的是,我收到以下错误。请看看。
ERROR TypeError: Cannot read property 'dismiss' of undefined
at LoginPage.push../src/app/login/login.page.ts.LoginPage.dismissLoader (login.page.ts:161)
at LoginPage.push../src/app/login/login.page.ts.LoginPage.doLogin (login.page.ts:103)
at Object.eval [as handleEvent] (LoginPage.html:8)
at handleEvent (core.js:23107)
at callWithDebugContext (core.js:24177)
at Object.debugHandleEvent [as handleEvent] (core.js:23904)
at dispatchEvent (core.js:20556)
at core.js:22046
at SafeSubscriber.schedulerFn [as _next] (core.js:13527)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:192)
我致电 this.dismissLoader(); 时遇到此错误,这是我的login.page.ts
import { Component, OnInit } from '@angular/core';
import { LoadingController } from '@ionic/angular';
@Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
loginFail: boolean = false;
errorMessage: string = '';
loading: any;
constructor(
private router : Router,
private loadingController: LoadingController) { }
ngOnInit() {
this.menu.enable(false);
}
doLogin(username:string, password:string){
this.showLoader();
if(username==='admin' && password==='admin'){
this.dismissLoader();
this.router.navigate(['/home']);
} else{
this.loginFail = true;
this.errorMessage = "Login failed";
this.dismissLoader();
}
}
async showLoader() {
this.loading = await this.loadingController.create({
message: 'Please wait...',
spinner: 'crescent'
});
this.loading.present();
}
dismissLoader() {
this.loading.dismiss();
}
有人可以帮我吗?
答案 0 :(得分:2)
@Component({
selector: 'exapmle-component',
templateUrl: './exapmle-component.html',
styleUrls: ['./exapmle-component.scss']
})
export class ExampleComponent {
public loading: HTMLIonLoadingElement;
constructor(
public loadingCtrl: LoadingController
) { }
public async createLoading(): Promise<boolean> {
if (this.loading) {
this.loading.dismiss();
}
const loadingOptions: LoadingOptions = {
animated: true,
backdropDismiss: false,
showBackdrop: true,
spinner: 'bubbles',
message: 'Hello'
};
this.loading = await this.loadingCtrl.create(loadingOptions);
this.loading.present();
return true;
}
public async dismissLoading(): Promise<void> {
if (this.loading) {
this.loading.dismiss();
}
}
public async submit(): Promise<void> {
await this.createLoading();
// Logic
this.dismissLoading();
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
尝试一下:
doLogin(username: string, password: string) {
const loading = await this.loadingController.create({
message: 'Please wait...',
spinner: 'crescent'
});
loading.present().then(() => {
if (username === 'admin' && password === 'admin') {
loading.dismiss();
this.router.navigate(['/home']);
} else {
this.loginFail = true;
this.errorMessage = "Login failed";
loading.dismiss();
}
}
}