Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:类型“Observable”上不存在属性“then”_Angular_Nativescript - Fatal编程技术网

Angular 角度:类型“Observable”上不存在属性“then”

Angular 角度:类型“Observable”上不存在属性“then”,angular,nativescript,Angular,Nativescript,我在这里使用Nativescript构建一个移动应用程序。这里有个错误。在我使用Visual Studio 2017开发ASP.Net MVC 5时,使用$http.get.then可以,但在Nativescript中则不行 请参见下面的代码: import { Component, OnInit } from "@angular/core"; import { HttpClient } from '@angular/common/http'; @Component({ selecto

我在这里使用Nativescript构建一个移动应用程序。这里有个错误。在我使用Visual Studio 2017开发ASP.Net MVC 5时,使用$http.get.then可以,但在Nativescript中则不行

请参见下面的代码:

import { Component, OnInit } from "@angular/core";
import { HttpClient } from '@angular/common/http';

@Component({
    selector: "Home",
    moduleId: module.id,
    templateUrl: "./home.component.html",
    styleUrls: ["./home.component.css"]
})
export class HomeComponent implements OnInit {

    constructor(private http: HttpClient) {
    }

    ngOnInit(): void {
        this.http.get('https://somewebsite.com',
            {
                params: { 'sorter': '', 'isAscending': 'true', 'searchString': '', 'currentPage': '1', 'itemsPerPage': '300' },
                headers: {
                    "Authorization": "Basic encryptedcodehere"
                }
            }).then(function (response) {
                var theData = JSON.parse(response.data);
                if (theData.Data != null && theData.Data.length > 0) {
                    log(theData.Data);
                }
            });
    }
}
错误是:类型“Observable”上不存在属性“then”

这里发生了什么?

。然后是承诺的方法。但您使用的是httpClient,它返回可观察的。您应该使用.subscribe而不是.then

你可以在官方文件中阅读更多关于和的信息。然后是承诺的方法。但您使用的是httpClient,它返回可观察的。您应该使用.subscribe而不是.then


您可以在官方文档中阅读更多信息,也可以这样做

import { Component, OnInit } from "@angular/core";
import { HttpClient } from '@angular/common/http';

@Component({
 selector: "Home",
 moduleId: module.id,
 templateUrl: "./home.component.html",
 styleUrls: ["./home.component.css"]
})
export class HomeComponent implements OnInit {

constructor(private http: HttpClient) {
}

ngOnInit(): void {
    this.http.get('https://somewebsite.com',
        {
            params: { 'sorter': '', 'isAscending': 'true', 'searchString': '', 
 'currentPage': '1', 'itemsPerPage': '300' },
            headers: {
                "Authorization": "Basic encryptedcodehere"
            }
        }).subscribe(data => { // no need to write the function you can simply create a arrow function too 
            var theData = JSON.parse(data);
            if (theData.Data != null && theData.Data.length > 0) {
                log(theData.Data);
            }
        });
  }
}

它抛出错误,因为这是promise中的一个方法,您也可以这样做

import { Component, OnInit } from "@angular/core";
import { HttpClient } from '@angular/common/http';

@Component({
 selector: "Home",
 moduleId: module.id,
 templateUrl: "./home.component.html",
 styleUrls: ["./home.component.css"]
})
export class HomeComponent implements OnInit {

constructor(private http: HttpClient) {
}

ngOnInit(): void {
    this.http.get('https://somewebsite.com',
        {
            params: { 'sorter': '', 'isAscending': 'true', 'searchString': '', 
 'currentPage': '1', 'itemsPerPage': '300' },
            headers: {
                "Authorization": "Basic encryptedcodehere"
            }
        }).subscribe(data => { // no need to write the function you can simply create a arrow function too 
            var theData = JSON.parse(data);
            if (theData.Data != null && theData.Data.length > 0) {
                log(theData.Data);
            }
        });
  }
}
它抛出错误,因为then是promise中的一种方法,使用subscribe可能是一种可行的方法。然而,如果您真的想使用then,如果您需要链接调用,它会很方便,那么您可以使用toPromise方法将您的可观察内容转换为承诺

使用subscribe可能是一个不错的选择。然而,如果您真的想使用then,如果您需要链接调用,它会很方便,那么您可以使用toPromise方法将您的可观察内容转换为承诺


尤金的回答是正确的。你也许可以查看文档了解更多细节。尤金的答案是正确的。您可以查看文档以了解更多详细信息。只有在您需要获得与某些旧代码或第三方代码兼容的承诺时,这种方法才是好的。否则建议使用.subscribe或.do。但是,如果您将使用.do来放置代码,则无论如何都应该调用.subscribe。它是如何运作的。它只能为订阅的用户返回数据。只有当您需要获得与某些旧代码或第三方代码兼容的承诺时,这种方法才是好的。否则建议使用.subscribe或.do。但是,如果您将使用.do来放置代码,则无论如何都应该调用.subscribe。它是如何运作的。它只能为订阅的用户返回数据。
this.http.get('https://somewebsite.com',
{
    //... 
}).toPromise().then(response => {
       //...
        });