如何从 ionic4 存储或 fat arrow 中获取 return 值?
How to return value from ionic4 storage or fat arrow?
我正在创建 ionic 4 angular 应用程序,并使用 ionic 4 存储。在那之后
我设置并获取键值 pair.now 当我获得键值离子存储时,我需要将值分配给一些变量和 return 一些变量。下面的代码我正在使用
//set key value pair
setId(){
this.storage.set('Id',this.id);
//get key value pair
getId(){
this.storage.get('Id').then((val)=>{
console.log('Id is:',val);
some_variable=val;
});
我需要 return 这个 some_variable。
//Like that
getId(){
this.storage.get('Id').then((val)=>{
console.log('Id is:',val);
return some_variable=val;
});
我知道我错了。如何在 ionic storage get 中实现这个 return 功能?
您在 getId()
- 函数中忘记了 return
getId(){
return this.storage.get('Id').then((val)=>{
console.log('Id is:',val);
return val;
});
提示:始终将预期的 return 类型写入您的函数。所以你的 IDE 可以抱怨,如果你错过了 return.
getId(): Promise<any> {
return this.storage.get('Id').then((val)=>{
console.log('Id is:',val);
return val;
});
现在您可以像这样调用您的函数来获取您的值:
this.getId().then(val => {
console.log('Here is your value.', val);
})
我正在创建 ionic 4 angular 应用程序,并使用 ionic 4 存储。在那之后 我设置并获取键值 pair.now 当我获得键值离子存储时,我需要将值分配给一些变量和 return 一些变量。下面的代码我正在使用
//set key value pair
setId(){
this.storage.set('Id',this.id);
//get key value pair
getId(){
this.storage.get('Id').then((val)=>{
console.log('Id is:',val);
some_variable=val;
});
我需要 return 这个 some_variable。
//Like that
getId(){
this.storage.get('Id').then((val)=>{
console.log('Id is:',val);
return some_variable=val;
});
我知道我错了。如何在 ionic storage get 中实现这个 return 功能?
您在 getId()
- 函数中忘记了 return
getId(){
return this.storage.get('Id').then((val)=>{
console.log('Id is:',val);
return val;
});
提示:始终将预期的 return 类型写入您的函数。所以你的 IDE 可以抱怨,如果你错过了 return.
getId(): Promise<any> {
return this.storage.get('Id').then((val)=>{
console.log('Id is:',val);
return val;
});
现在您可以像这样调用您的函数来获取您的值:
this.getId().then(val => {
console.log('Here is your value.', val);
})