Javascript 离子2中的本地存储无法访问then()方法之外的数据

Javascript 离子2中的本地存储无法访问then()方法之外的数据,javascript,angular,typescript,promise,ionic2,Javascript,Angular,Typescript,Promise,Ionic2,我在学离子2。我在SomeComponent中的ionic local storage中存储了一些数据,并在UserService中检索相同的数据以调用后端但storage。get()方法返回Promise,因此我无法从中获取数据,因为我了解了promises。有谁能帮助我更好地完成这项工作吗?如果有的话,谢谢你 这是我的密码 export class SomeComponent { user: User; usename: string; password: string; c

我在学离子2。我在SomeComponent中的ionic local storage中存储了一些数据,并在UserService中检索相同的数据以调用后端但
storage。get()
方法返回Promise,因此我无法从中获取数据,因为我了解了promises。有谁能帮助我更好地完成这项工作吗?如果有的话,谢谢你

这是我的密码

export class SomeComponent {

 user: User;
 usename: string;
 password: string;

   constructor(
    public navParams: NavParams,
    public navController: NavController, 
    public loginService: LoginSevice,
    public storage: Storage) {

   }

    login() {
        this.loginService.login(this.username, this.password).subscribe( response => {
          this.user = response;
          this.storage.set('userId', this.user.userId);
          this.storage.set( 'authcode', this.user.authCode);
        });
      }
}
服务是

    export class UserService {

      userId: string;
      constructor(public http: Http, public storage: Storage){}

      fetchUserInfo(): Observable<User> { 

          this.storage.get('userId').then((val) => {
            console.log(val); // available only inside then() method
            this.userId = val; 
          });

          let user = btoa(this.userId); // userId is undefined
          return this.http.get(`${this.baseUrl}/user/${user}/details`, {headers: 
               this.headers})
             .map(res =>res.json());
         }
      }
   }
导出类用户服务{
userId:string;
构造函数(公共http:http,公共存储:存储){}
fetchUserInfo():可观察的{
this.storage.get('userId')。然后((val)=>{
console.log(val);//仅在then()方法中可用
this.userId=val;
});
让user=btoa(this.userId);//userId未定义
返回此.http.get(`${this.baseUrl}/user/${user}/details`,{头:
此文件(0.0})
.map(res=>res.json());
}
}
}

解决此问题的一种方法如下:

export class UserService {

    userId: string;
    constructor(public http: Http, public storage: Storage){}

    fetchUserInfo(): Observable<User> { 

        return Observable
            .fromPromise(this.storage.get('userId'))
            .flatMap(userId => 
                this.userId = val;

                let user = btoa(this.userId);
                return this.http.get(`${this.baseUrl}/user/${user}/details`, {headers: this.headers})
                           .map(res =>res.json());
    }
}
导出类用户服务{
userId:string;
构造函数(公共http:http,公共存储:存储){}
fetchUserInfo():可观察的{
可观测回波
.fromPromise(this.storage.get('userId'))
.flatMap(userId=>
this.userId=val;
let user=btoa(this.userId);
返回this.http.get(`${this.baseUrl}/user/${user}/details`,{headers:this.headers})
.map(res=>res.json());
}
}

这样,我们首先创建一个observable,承诺从存储返回值(
userId
),然后使用该
userId
我们发出http请求(将返回
observable
)。您可以找到有关
flatmap
操作符的更多信息。

我尝试了
mergeMAp
这也非常有效。我不知道同时使用
flatmap
mergMap
的区别,但它确实有效

export class UserService {
    userId: string;
    constructor(public http: Http, public storage: Storage){}
    fetchUserInfo(): Observable<User> { 
    return Observable
          .fromPromise(this.storage.get('userId')).mergeMap(userId => 
              this.userId = val;
              let user = btoa(this.userId);
              return this.http.get(`${this.baseUrl}/user/${user}/details`, {headers: this.headers})
                               .map(res =>res.json());
    }
}
导出类用户服务{
userId:string;
构造函数(公共http:http,公共存储:存储){}
fetchUserInfo():可观察的{
可观测回波
.fromPromise(this.storage.get('userId')).mergeMap(userId=>
this.userId=val;
let user=btoa(this.userId);
返回this.http.get(`${this.baseUrl}/user/${user}/details`,{headers:this.headers})
.map(res=>res.json());
}
}

可能重复@echonax Hi Sir,是否有其他方法在ionic中存储和检索本地存储的数据。我对ionic不是很有经验。但是官方文档建议使用它,所以我认为这是最好的。@echonax感谢您的即时回复。您能解释一下如何从那时起回复我的承诺吗()解析为字符串值的函数。您应该阅读链接:-)