ionic 3 在函数ionViewDidLoad()中读取存储值native storage
ionic 3 read stored value native storage in function ionViewDidLoad ()
我有问题,我想在函数 -> ionViewDidLoad() 中读取一个保存在 Native Storage Cordova 插件中的值。
函数说明:
ionViewDidLoad () = "Runs when the page has loaded. This event only
happens once per page being created. If a page leaves but is cached,
then this event will not fire again on a subsequent viewing. The
ionViewDidLoad event is good place to put your setup code for the
page."
我尝试执行的操作并在打开应用程序时显示一条消息。但是如果用户不希望每次应用启动时都打开,我想在ionViewDidLoad()函数中做一个条件,让它按照用户的意愿打开。
Home.ts 页数
import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { ParamProviders } from '../../providers/paramProviders';
import { NativeStorage } from '@ionic-native/native-storage';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [ParamProviders]
})
export class HomePage {
boolBienvenue: boolean;
constructor(public navCtrl: NavController, private alertCtrl: AlertController, private nativeStorage: NativeStorage, private paramService: ParamProviders) {
}
ionViewDidLoad() {
this.boolBienvenue1()
console.log(this.boolBienvenue);
if ( this.boolBienvenue == true ) {
this.demarrage();
}
}
public boolBienvenue1() {
this.nativeStorage.getItem('storage_pref')
.then(
data => this.boolBienvenue = data.bienvenue,
error => console.error(error)
)
}
public demarrage() {
let alert = this.alertCtrl.create({
title: 'Bienvenue dans HackChat !!',
message: 'Avant de discuter avec vos amies ou vos proches, veuillez configurer l\'application en y indiquant l\'adresse de votre serveur ainsi que votre login et mot de pass dans l\'onglet Paramétres. Une fois que vous serrez connecté, vous pourrez discuter.<br />Allez y c\'est à vous !!<br /><br />Voulez vous que cette bulle d\'information s\'affiche lors de l\'ouverture de HackChat ?',
buttons: [
{
text: 'Non',
handler: () => {
console.log('Disagree clicked');
}
},
{
text: 'Oui',
handler: () => {
console.log('Agree clicked');
}
}
]
});
alert.present();
}
}
Parameter.ts 页数
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import { NativeStorage } from '@ionic-native/native-storage';
/*
Generated class for the PeopleSearch provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class ParamProviders {
linkServer: string ;
user: string;
password: string;
bienvenue: boolean = true;
notification: boolean = false;
vibration: boolean = false;
constructor(public http: Http, private nativeStorage: NativeStorage) {
}
public savePref(): void {
this.nativeStorage.setItem('storage_pref', {
linkServer: this.linkServer,
user: this.user,
password: this.password,
bienvenue: this.bienvenue,
notification: this.notification,
vibration: this.vibration
})
.then(
() => console.log(this.linkServer),
error => console.error('Error storing item', error)
);
}
public loadPref():void {
this.nativeStorage.getItem('storage_pref')
.then(
data => {
this.linkServer = data.linkServer;
this.user = data.user;
this.password = data.password;
this.bienvenue = data.bienvenue;
this.notification = data.notification;
this.vibration = data.vibration;
},
error => console.error(error)
);
}
}
你能帮帮我吗
谢谢
访问存储是一个异步作业,因此您的以下代码很可能不起作用,因为在您尝试询问您的布尔值是否已设置后,异步作业的结果已解析
ionViewDidLoad() {
this.boolBienvenue1() // Here you don't wait for async job
console.log(this.boolBienvenue); // So this value might or might not be set
if ( this.boolBienvenue == true ) {
this.demarrage();
}
}
public boolBienvenue1() {
this.nativeStorage.getItem('storage_pref')
.then(
data => this.boolBienvenue = data.bienvenue,
error => console.error(error)
)
}
例如,您可以按如下方式修改代码:
ionViewDidLoad() {
this.nativeStorage.getItem('storage_pref')
.then(
(data) => {
if ( data !== null && data.bienvenue ) {
this.demarrage();
}
}
error => console.error(error)
)
}
P.S.: 与你的问题无关,但如果可以的话,在你不需要添加 == true
的条件下测试布尔值是否为真,布尔值本身是足够。我的意思是:
if (something == true)
应该写成
if (something)
我有问题,我想在函数 -> ionViewDidLoad() 中读取一个保存在 Native Storage Cordova 插件中的值。
函数说明:
ionViewDidLoad () = "Runs when the page has loaded. This event only happens once per page being created. If a page leaves but is cached, then this event will not fire again on a subsequent viewing. The ionViewDidLoad event is good place to put your setup code for the page."
我尝试执行的操作并在打开应用程序时显示一条消息。但是如果用户不希望每次应用启动时都打开,我想在ionViewDidLoad()函数中做一个条件,让它按照用户的意愿打开。
Home.ts 页数
import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { ParamProviders } from '../../providers/paramProviders';
import { NativeStorage } from '@ionic-native/native-storage';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [ParamProviders]
})
export class HomePage {
boolBienvenue: boolean;
constructor(public navCtrl: NavController, private alertCtrl: AlertController, private nativeStorage: NativeStorage, private paramService: ParamProviders) {
}
ionViewDidLoad() {
this.boolBienvenue1()
console.log(this.boolBienvenue);
if ( this.boolBienvenue == true ) {
this.demarrage();
}
}
public boolBienvenue1() {
this.nativeStorage.getItem('storage_pref')
.then(
data => this.boolBienvenue = data.bienvenue,
error => console.error(error)
)
}
public demarrage() {
let alert = this.alertCtrl.create({
title: 'Bienvenue dans HackChat !!',
message: 'Avant de discuter avec vos amies ou vos proches, veuillez configurer l\'application en y indiquant l\'adresse de votre serveur ainsi que votre login et mot de pass dans l\'onglet Paramétres. Une fois que vous serrez connecté, vous pourrez discuter.<br />Allez y c\'est à vous !!<br /><br />Voulez vous que cette bulle d\'information s\'affiche lors de l\'ouverture de HackChat ?',
buttons: [
{
text: 'Non',
handler: () => {
console.log('Disagree clicked');
}
},
{
text: 'Oui',
handler: () => {
console.log('Agree clicked');
}
}
]
});
alert.present();
}
}
Parameter.ts 页数
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import { NativeStorage } from '@ionic-native/native-storage';
/*
Generated class for the PeopleSearch provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class ParamProviders {
linkServer: string ;
user: string;
password: string;
bienvenue: boolean = true;
notification: boolean = false;
vibration: boolean = false;
constructor(public http: Http, private nativeStorage: NativeStorage) {
}
public savePref(): void {
this.nativeStorage.setItem('storage_pref', {
linkServer: this.linkServer,
user: this.user,
password: this.password,
bienvenue: this.bienvenue,
notification: this.notification,
vibration: this.vibration
})
.then(
() => console.log(this.linkServer),
error => console.error('Error storing item', error)
);
}
public loadPref():void {
this.nativeStorage.getItem('storage_pref')
.then(
data => {
this.linkServer = data.linkServer;
this.user = data.user;
this.password = data.password;
this.bienvenue = data.bienvenue;
this.notification = data.notification;
this.vibration = data.vibration;
},
error => console.error(error)
);
}
}
你能帮帮我吗 谢谢
访问存储是一个异步作业,因此您的以下代码很可能不起作用,因为在您尝试询问您的布尔值是否已设置后,异步作业的结果已解析
ionViewDidLoad() {
this.boolBienvenue1() // Here you don't wait for async job
console.log(this.boolBienvenue); // So this value might or might not be set
if ( this.boolBienvenue == true ) {
this.demarrage();
}
}
public boolBienvenue1() {
this.nativeStorage.getItem('storage_pref')
.then(
data => this.boolBienvenue = data.bienvenue,
error => console.error(error)
)
}
例如,您可以按如下方式修改代码:
ionViewDidLoad() {
this.nativeStorage.getItem('storage_pref')
.then(
(data) => {
if ( data !== null && data.bienvenue ) {
this.demarrage();
}
}
error => console.error(error)
)
}
P.S.: 与你的问题无关,但如果可以的话,在你不需要添加 == true
的条件下测试布尔值是否为真,布尔值本身是足够。我的意思是:
if (something == true)
应该写成
if (something)