简体   繁体   English

Angular 8 Observable返回“ _isScalar:false…”

[英]Angular 8 Observable Returns “_isScalar:false…”

Just trying to simply display the contents of a JSON object from a GET request. 只是尝试简单地显示来自GET请求的JSON对象的内容。

Service: 服务:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class LightsService {

  constructor(private http: HttpClient) {}

  fetchLights(): Observable<Object> {
    const URL = 'http://****/api/S97t-zlmOCIeKXxQzU66WxWLY2z6oKenpLM95Uvt/lights';
    console.log('Service');
    return this.http.get(URL);
  }
}

Component: 零件:

import { Component } from '@angular/core';
import { LightsService } from './lights.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less']
})
export class AppComponent {
  lights;
  constructor(private lightsService: LightsService) {}

  fetchLights() {
    console.log('Component');
    this.lights = this.lightsService.fetchLights();
    console.log(this.lights);
  }
}

HTML: HTML:

<button (click)="fetchLights()">Fetch Lights</button>

<ul>
  <li *ngFor="let light of lights | keyvalue">{{light.key}}:{{light.value}}</li>
</ul>

I'd prefer to not have to use the 'keyvalue' pipe but it seems to be the only way to get anything returned at all, however here is a screenshot of what gets returned when the function is called: 我希望不必使用'keyvalue'管道,但这似乎是获得所有返回值的唯一方法,但是这是调用函数时返回的屏幕快照:

在此处输入图片说明

An observable can not be used as a value. 可观察值不能用作值。

You have to use the pipe async to get the value of an observable. 您必须使用管道async来获取可观察值。

To have a clean solution, it's better to create an Interface of what service return: 为了获得一个干净的解决方案,最好创建一个返回服务的接口:

interface Lights{
    label1: string;
    label2: string;
    ect ect
}

Than in your service file: 比在您的服务文件中:

fetchLights(): Observable<Lights> {
    const URL = 'http://****/api/S97t-zlmOCIeKXxQzU66WxWLY2z6oKenpLM95Uvt/lights';
    console.log('Service');
    return this.http.get<Lights>(URL);
  }

You need to subscribe your observable in AppComponent like this: 您需要像这样在AppComponent中订阅您的可观察对象:

public lights: Lights;

fetchLights() {
    console.log('Component');
   this.lightsService.fetchLights().subscribe((lights: Lights) => {
   this.lights = lights
  });

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 6中的RXJS仅返回{“ _isScalar”:false} - RXJS in Angular 6 returns only { “_isScalar”: false } rxjs observable.of(object) 返回 {“_isScalar”: “”, “value”: {}, “scheduler”: “” } 而不是 value - rxjs observable.of(object) returns {“_isScalar”: “”, “value”: {}, “scheduler”: “” } instead of value Angular6获取方法响应“ _isScalar”:false,“源” - Angular6 Get method response “_isScalar”:false,“source” “MyModel”类型缺少“Observable”类型的以下属性<mymodel> ': Angular 中的 _isScalar、source、operator、lift 等 6 个</mymodel> - Type 'MyModel' is missing the following properties from type 'Observable<MyModel>': _isScalar, source, operator, lift, and 6 more in Angular Angular Observable不返回任何数据 - Angular Observable returns no data Angular 7可观察的等待变量为false - Angular 7 observable wait for variable to be false req.isAuthenticated始终在angular2应用中使用observable返回false - req.isAuthenticated always returns false with using observable in angular2 app 在Angular中可观察到的返回[Object object] - Observable in Angular returns [ Object object ] Angular 2 Service Observable返回未定义 - Angular 2 Service Observable returns undefined 角度4可观察到的回报[object Object] - angular 4 observable returns [object Object]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM