Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 为什么我没有定义值_Angular_Typescript_Ionic Framework_Ionic3 - Fatal编程技术网

Angular 为什么我没有定义值

Angular 为什么我没有定义值,angular,typescript,ionic-framework,ionic3,Angular,Typescript,Ionic Framework,Ionic3,我正在学习Angular4。我有一些我不理解的东西。 我将一个值存储在服务提供者上,然后从组件获取它。 这是我的代码: gard服务提供商: key:any; constructor(){} storeKeysAppPreferences(res){ this.appPreferences.clearAll(); console.log("storeKeysAppPreferences",res); this.app

我正在学习Angular4。我有一些我不理解的东西。 我将一个值存储在服务提供者上,然后从组件获取它。 这是我的代码:

gard服务提供商:

key:any;
constructor(){}
    storeKeysAppPreferences(res){
            this.appPreferences.clearAll();
            console.log("storeKeysAppPreferences",res);
            this.appPreferences.store('key1',JSON.stringify(res));
        }

        fetchKeysAppPreferences(){
          this.appPreferences.fetch("key1").then(
            (res) => {
                this.key=(JSON.parse(res));
              }
           );
        }
当我尝试
console.log()


键的值未定义。为什么会这样?

您必须将
console.log(this.key);//回调中未定义的
实际上,第二个控制台.log()是在响应准备就绪之前执行的。

由于它是异步操作,您需要执行以下操作:

注意:此处仅显示概念。请根据您的用例安排方法

constructor(){}
    storeKeysAppPreferences(res){
            this.appPreferences.clearAll();
            console.log("storeKeysAppPreferences",res);
            this.appPreferences.store('key1',JSON.stringify(res)).then(()=>{//here is the place
                  fetchKeysAppPreferences(){
                      this.appPreferences.fetch("key1").then(
                         (res) => {
                            this.key=(JSON.parse(res));
                          }
                      );
                    }
                    })
          }

冷静点,里面没有
http
word。它可以是具有相同关键字的自定义类。哦,你的
appPreferences
类做什么?它是在进行http调用,还是本地存储调用,还是?应用程序引用基本上是一个我们存储在上面的文件,比如tokens@trichetriche无论代码中是否有
http
,它都是重复的。promise
then
调用的回调根据定义是异步的要解决此问题吗?等等,在获取密钥属性后,您想对其执行什么操作?我将从另一个组件(主页)调用获取来测试密钥。键表示令牌。请尝试以下操作:-->let key:any=fetchKeysAppPreferences(){this.appPreferences.fetch(“key1”)。然后((res)=>{return(JSON.parse(res));});否则必须返回此.appPreferences.fetch(“key1”)从方法中调用。然后在必须使用它的地方storeKeysAppPreferences和fetchKeysAppPreferences是两个独立的方法。我在登录页面上调用storeKeysAppPreferences,在主页上调用fetchKeysAppPreferences。所以我不能这样做,我只是展示了概念。这似乎是异步模式的问题。所以我建议您使用
push/setRoot
方法发送数据。
constructor(){}
    storeKeysAppPreferences(res){
            this.appPreferences.clearAll();
            console.log("storeKeysAppPreferences",res);
            this.appPreferences.store('key1',JSON.stringify(res)).then(()=>{//here is the place
                  fetchKeysAppPreferences(){
                      this.appPreferences.fetch("key1").then(
                         (res) => {
                            this.key=(JSON.parse(res));
                          }
                      );
                    }
                    })
          }