繁体   English   中英

angular2 / 4可观察订阅

[英]angular2/4 Observable subscription

我试图对我的服务器数据库进行api调用,但它不能正常工作。

我有以下错误:

在此输入图像描述

    Type 'Subscription' is not assignable to type 'Observable<Comment[]>'.
  Property '_isScalar' is missing in type 'Subscription'.

我做错了什么,为什么?

import { Injectable } from '@angular/core';
import { Http, RequestOptions, URLSearchParams } from '@angular/http';
import {Observable} from 'rxjs/Observable';
import { Comment } from './models/comment'
import 'rxjs/add/operator/map';


@Injectable()
export class CoreService {

  constructor(private http: Http, private URLSearchParams : URLSearchParams) { }

  options = [];
  result = [];

  getUserByName(name: string): Observable<Comment[]> {

    let url = "http://localhost:8080/api";


    return this.http.get(url)
        .map(response => response.json())
        .subscribe(result => this.result = response);
  }

}

评论:

    export class Comment {
    constructor(
        public text:string
    ){}
}

我想您正在尝试在组件中订阅getUserByName()

如果是这种情况,您应该从您的服务中删除订阅。

您可以使用do()运算符更改它以“缓存”您的数据。

return this.http.get(url)
        .map(response => response.json())
        .do(result => this.result = response);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM