我试图对我的服务器数据库进行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
){}
}
答案 0 :(得分:4)
我认为您正在尝试订阅组件中的getUserByName()
。
如果是这种情况,您应该从服务中删除订阅。
您可以使用do()
运算符将其更改为&#34;缓存&#34;你的数据。
return this.http.get(url)
.map(response => response.json())
.do(result => this.result = response);