簡體   English   中英

Ionic2 AppPreferences未定義

[英]Ionic2 AppPreferences undefined

我有以下代碼從Ionic2本機AppPreferences獲取城市信息:

import { AppPreferences } from '@ionic-native/app-preferences';

constructor(public appPreferences: AppPreferences) {

    console.log("The city is : " + this.loadPreferences("selectedCity"));

}

loadPreferences(preferenceKey: any){
    this.appPreferences.fetch(preferenceKey).then((res) => { return res; });
}

內部.then((res)=> {console.log}); 該值已打印並且可以正常工作。 但是,當我返回值或將其分配給某個變量時,我得到的值是“城市是:未定義”。 我做錯什么了嗎?

謝謝和提前問候。

我知道了。 就像@ misha130所說的那樣,想想每一行。 在從異步方法獲取結果之前,將打印以下行:

console.log("The city is : " + this.loadPreferences("selectedCity"));

我更改了一點代碼。 現在,當我在異步函數中獲得結果時,運行我的代碼。 非常感謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM