显示加载,警报和日志的ionic2最佳实践是什么? [英] What is the ionic2 best practice to show loading, alerts and logs?
问题描述
我正在寻找ionic2最佳做法来显示加载,警报和控制台日志..
I'm looking for ionic2 best practice to show loadings, alerts and console logs..
所以不要在每个页面重复下面的代码,这样我就可以调用一次。
so instead of repeat the code below in each page so I can call it once.
例如显示加载的代码:
showLoading() {
this.loading = this.loadingCtrl.create({
content: ''
});
this.loading.present();
}
这是创建提供商的最佳做法,并显示来自持续?或提供者不支持加载或类似的东西?
Is it the best practice to create a provider, and show the loading from the last? or the provider doesn't support loading or something similar ?
谢谢。
推荐答案
我通常会创建一个lib文件夹(或模块,如果你想要的话)。然后,我创建了一些提供者。如需提醒,您可以创建提供商:
I usually create a lib folder (or module if you want). Then, I create some providers. For alert, you can create a provider:
import { Injectable } from '@angular/core';
import { AlertController } from 'ionic-angular';
@Injectable()
export class Alert {
constructor(
public alertCtrl: AlertController
) {}
show(title: string, subTitle: string, buttons: Array<string>): void{
let alert = this.alertCtrl.create({
title: title,
subTitle: subTitle,
buttons: buttons
});
alert.present();
}
}
或者加载例如:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { LoadingController } from 'ionic-angular';
@Injectable()
export class Loader {
loader: any;
constructor(
public http: HttpClient,
public loadingCtrl: LoadingController
) {}
present(msg = `Please wait...`) {
this.loader = this.loadingCtrl.create({
content: "Please wait..."
});
this.loader.present();
}
dismiss() {
this.loader.dismiss();
}
}
然后,您可以在任何带有模块的组件中重复使用路径映射例如:
And then, You can reuse in any components with module path mapping for example:
import { Alert, Toast } from '@lib';
这是最佳做法吗?是的,我认为。你写的代码少了,你可以重用你的lib提供者。
Is this a best practice ? Yes, I think. You write less code and yo can reuse your lib providers.
希望我能帮忙!
这篇关于显示加载,警报和日志的ionic2最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!