无法使用.subscribe方法在本地存储

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用.subscribe方法在本地存储相关的知识,希望对你有一定的参考价值。

我的代码有问题(我可能误解了一些东西)。

我使用angular 5,这里是代码:

public readZWaveState(zwave_cmd:String):Observable<String>{     
  return this.http
       .get("http://ipAdd/core/api/jeeApi.php?apikey=xxx&type=cmd&id="+zwave_cmd)
       .map(res => res.json());}

我在另一个文件中订阅此方法,如下所示:

this.the4beesRestService.readZWaveState("xx").subscribe(
        res => {   
            this.l1_state = res;     
        });

这就是问题所在。我可以使用{{l1_state}}访问html代码中的值,但我无法在本地访问它。

我尝试使用console.log(this.l1_state),但它返回undefined。

我认为这是一个范围问题,所以如果有人可以帮助我,这将是伟大的。

答案

使用以下代码尝试:

this.the4beesRestService.readZWaveState("xx").subscribe((res) => {   
            this.l1_state = res;     
            console.log(this.l1_state);
        });

以上是关于无法使用.subscribe方法在本地存储的主要内容,如果未能解决你的问题,请参考以下文章