简体   繁体   English

在ES5代码中使用Angular2 HTTP服务

[英]Using Angular2 HTTP Service in ES5 code

How to use the HTTP service provided by Angular2 in ES5 code? 如何在ES5代码中使用Angular2提供的HTTP服务? Here is the TypeScript example. 是TypeScript示例。

I basically need examples of GET and POST requests. 我基本上需要GET和POST请求的示例。

The example shown here is not working. 此处显示的示例无效。 get seems to be undefined. get似乎是不确定的。

Here is an example that doesn't work: 这是一个不起作用的例子:

    var SearchResultComponent = ng.core.Component({
        selector: "search-result",
        viewProviders: [ng.http.HTTP_PROVIDERS],
        templateUrl: "componentTemplates/SearchResult.html"
    }).Class({
        constructor: [ng.http.HTTP_PROVIDERS, function(http) {
            this.params = params;
            this.http = http;
        }],
        ngOnInit: function(){

            console.log(this.http.get); //undefined
        }
    })

In the constructor of your SearchResultComponent component, you need use ng.http.Http and not HTTP providers: SearchResultComponent组件的构造函数中,您需要使用ng.http.Http而不是HTTP提供程序:

var SearchResultComponent = ng.core.Component({
    selector: "search-result",
    viewProviders: [ng.http.HTTP_PROVIDERS],
    templateUrl: "componentTemplates/SearchResult.html"
}).Class({
    constructor: [ng.http.Http, function(http) { // <-----
        this.params = params;
        this.http = http;
    }],
    ngOnInit: function(){

        console.log(this.http.get); //undefined
    }
});

In your case, it wasn't the Http object that was injected... 在你的情况下,它不是注入的Http对象......

Providers can be specified when boostrapping your application, in the providers attribute or the viewProviders attribute (like you did). 在提升应用程序时,可以在providers属性或viewProviders属性中指定providers (就像您一样)。

See this plunkr for a use of HTTP with Angular2 and ES5: https://plnkr.co/edit/oM4gTgW5Q7gpR8Qderjj . 有关使用Angular2和ES5的HTTP的信息,请参阅此plunkr: https ://plnkr.co/edit/oM4gTgW5Q7gpR8Qderjj。

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

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