2016-09-26 73 views
1

由於ng.http.HTTP_PROVIDERS不贊成使用ng.http.HttpModule,因此無法獲取HTTP GET請求以在我的組件中工作。在ES5中使用Angular2最終版本2.0.1的http示例

這裏的組件:

(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 ([email protected]:418) 

回答

2

你必須包括在範圍內正確的順序angular2腳本您html:

<script src="https://unpkg.com/[email protected]/client/shim.min.js"></script> 
<script src="https://unpkg.com/[email protected]/dist/zone.min.js"></script> 
<script src="https://unpkg.com/[email protected]/bundles/Rx.min.js"></script> 

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

Plunker Example Http request in ES5

+0

你救了我的一天。 – kaotik

+0

謝謝,主席先生,我絕望地拉着頭髮。 –

相關問題