gpt4 book ai didi

angular2/4 可观察订阅

转载 作者:太空狗 更新时间:2023-10-29 18:20:19 25 4
gpt4 key购买 nike

我正在尝试对我的服务器数据库进行 api 调用,但它目前无法正常工作。

我有以下错误:

enter image description here

    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);

关于angular2/4 可观察订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43678368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com