首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“TypeScript SomeArray[]”不能赋值给类型“Observable<SomeArray[]>”

“TypeScript SomeArray[]”不能赋值给类型“Observable<SomeArray[]>”
EN

Stack Overflow用户
提问于 2017-02-01 10:40:30
回答 2查看 741关注 0票数 1

我冒险进入Angular 2和TypeScript的世界,并遇到了这个错误。看起来你可以在某个地方设置_isScalar。只是不确定这应该放在哪里。

我得到以下错误(参见代码中的注释行):

类型'Rating[]‘不可赋值给类型'Observable’。类型'Rating[]‘中缺少属性'_isScalar’

以获取以下代码:

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Inject } from '@angular/core';
import { Jsonp, JsonpModule, Http } from '@angular/http';
import { Rating } from './rating.model';
import { Observable } from 'rxjs/Rx';
import { DOCUMENT } from '@angular/platform-browser'
import { IODataList } from '@microsoft/sp-odata-types';
import { SPHttpClient, SPHttpClientConfigurations, SPHttpClientConfiguration, SPHttpClientResponse, ODataVersion, ISPHttpClientConfiguration } from '@microsoft/sp-http';

@Injectable()
// See: http://www.telerik.com/kendo-angular-ui/components/dropdowns/dropdownlist/#toc-data-binding

export class RatingsService {
//constrauctor( private jsonp: Jsonp) { }
    constructor(@Inject(Http) private http: Http, @Inject(DOCUMENT) private document: any) { }

    //var url = this.context.pageContext.web.absoluteUrl + `/_api/web/lists?$filter=Hidden eq false`;

    private choices: Array<Rating> = [];

    public fetchData(): Observable<Rating[]> {

        console.log("BGW: Document location = " + document.location.href);
    // var url = this.context.pageContext.web.absoluteUrl + `/_api/web/lists?$filter=Hidden eq false`;
        //var response = this.jsonp.get(url); //.map((response => response.json()));
        console.log("BGW: Ratings choices url = " + url); 

        //var choices: Array<Rating> = new Array<Rating>();
        var choices: Array<Rating> = new Array<Rating>();
        // choices._isScalar = true;
        //let choices: Observable<Rating[]> = new Observable<Rating>();

        if (document.location.href.indexOf("localhost") > 0)
        {
            // Send dummy data to the form.
            console.log("BGW: Local workbench in use");
            choices.push(new Rating("High"));
            choices.push(new Rating("Medium"));
            choices.push(new Rating("Low"));
            return choices; // *** ISSUE HERE ***
        }
        else
        {
            var url = "https://somewhere.sharepoint.com/sites/Development/Subsite/_api/web/lists/GetByTitle('Requirements')/fields?$filter=EntityPropertyName%20eq%20%27Requirement_x0020_Rating%27";
            console.log("BGW: Ratings choices url = " + url);
            // Ref: http://chariotsolutions.com/blog/post/angular2-observables-http-separating-services-components/
            var response = this.http.get(url).map((response => response.json()));
            console.log("BGW: response = " + JSON.stringify(response));

            return response.map((choices: Array<any>) => {
                if (choices) {
                    choices.forEach((choice) => {
                        choices.push(new Rating(choice));
                    });
                }
                return choices;
            });
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2017-02-02 06:33:08

这个问题是因为您没有像您指定的那样返回一个可观察对象:

代码语言:javascript
复制
public fetchData(): Observable<Rating[]>

但稍后你会定义...

代码语言:javascript
复制
var choices: Array<Rating> = new Array<Rating>(); 

这就是你得到这个错误的原因。可观察数组和“常规”数组不匹配。如果您不显式地需要来指定您要返回的内容,我将完全删除Observable<Rating[]>

票数 2
EN

Stack Overflow用户

发布于 2017-02-01 11:10:51

你需要返回一个可观察的,比如:

代码语言:javascript
复制
return Observable.of<Rating[]>(choices);

它使用静态方法of,但还有许多其他方法。

您看到编译器错误的原因是因为您在方法签名中指定了返回类型:

代码语言:javascript
复制
public fetchData(): Observable<Rating[]>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41971053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档