簡體   English   中英

在ES5中使用Angular2最終版本2.0.1進行http獲取的示例

[英]Example of http get using Angular2 final release 2.0.1 in ES5

由於不建議使用ng.http.HTTP_PROVIDERS,而建議使用ng.http.HttpModule,因此無法獲取HTTP GET請求以在組件中工作。

這是組件:

(function (app) {
app.ServiceComponent = ng.core
    .Component({
        selector: 'service',
        templateUrl: 'app/service/service.component.html',
        directives: [ ng.router.ROUTER_DIRECTIVES, app.NestedComponent ],
        providers: [app.TestService, ng.http.HttpModule ]
    })
    .Class({
        constructor: [app.TestService, function(testService) {
            this.title = 'Service Page Test';
            this.body =  'Service Body Test';
            this.message = '';
            this.xhr = {};
            this.testService = testService;
        }],
        ngOnInit: function() {
            this.message = this.testService.getMessage();
            this.xhr = this.testService.getData();
        }
    });
})(window.app || (window.app = {}));

這是服務:

(function(app) {

app.TestService = function (http) {
    this.message = 'Hello Message Test';
    this.url = "service.json";
    this.http = http;
};


app.TestService.parameters = [ ng.http.Http ];

app.TestService.prototype.getMessage = function () {
    return this.message;
};

app.TestService.prototype.setMessage = function (newMessage) {
    this.message = newMessage;
};

app.TestService.prototype.getData = function () {
    return this.http.get(this.url)
        .map(function (response) {
            return response.json().data;
        })
        .catch();
   };})(window.app || (window.app = {}));

我得到錯誤:

core.umd.js:3433 Error: Uncaught (in promise): TypeError: Cannot read property '__platform_browser_private__' of undefined
at resolvePromise (zone@0.6.23.js:418)

您必須以正確的順序在html中包含angular2腳本:

<script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.6.21/dist/zone.min.js"></script>
<script src="https://unpkg.com/rxjs@5.0.0-beta.12/bundles/Rx.min.js"></script>

<script src="https://unpkg.com/@angular/core@2.0.1/bundles/core.umd.js"></script>
<script src="https://unpkg.com/@angular/common@2.0.1/bundles/common.umd.js"></script>
<script src="https://unpkg.com/@angular/platform-browser@2.0.1/bundles/platform-browser.umd.js"></script>
<script src="https://unpkg.com/@angular/compiler@2.0.1/bundles/compiler.umd.js"></script>
<script src="https://unpkg.com/@angular/platform-browser-dynamic@2.0.1/bundles/platform-browser-dynamic.umd.js"></script>
<script src="https://unpkg.com/@angular/http@2.0.1/bundles/http.umd.js"></script>

ES5中的Plunker示例Http請求

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM