如何读取订阅中的对象(可观察)-角度6

时间:2019-03-06 14:00:10

标签: angular typescript angular6 observable subscribe

我正在开发一个角度为6的项目,现在正面临一个问题! 我想读取可观察对象的内容,如下所示:

this.myService.getOne().subscribe(e => {
  this.e = e;
  console.log(this.e);
  console.log(this.e.id); // result => undefined
});

我可以看到 this.e ,如下所示:

{"id":"1", "name":"myName", ...}

但是 this.e.id 'undefined',当我写 this.e ['id'] 时也是一样。< / p>

你有什么主意吗?

1 个答案:

答案 0 :(得分:0)

我发现了这个问题,我的getOne()方法中还有两件事:

我从更改了方法

getOne(): Observable<myObject> {...}

getOne() {...}

我更改了退货

this.get<myObject>(url, httpOptions); //httpOptions contained headers and response type 'text' as 'json' 

this.get<myObject>(url);

现在功能正常,谢谢您的帮助! :)

相关问题