2016-08-02 37 views
0

我正在使用Angular2和es5。我想在服務中使用http。 不幸的是我有2個錯誤: - HTTP是不確定的,但ng.http.Http定義, - 我有這個錯誤的主要成分:http與Angular2的服務與es5

vendor-client.min.js:28 EXCEPTION: Can't resolve all parameters for class0: (t, ?) 

這裏是我的服務代碼:

;(function(app, ng) { 
    console.log(new ng.http.Http()); 

    app.ApplicationsService = ng.core.Injectable().Class({ 
    constructor: [ng.http.Http, function(http) { 
     console.log(http); 
     this.applicationsEmailUrl = 'api/applications/email'; 
     this.http = http; 
    }], 

    emailExists: function(email) { 
     console.log(email); 
     var data = { email: email }; 
     return this.http.post(this.applicationsEmailUrl, data) 
     .toPromise() 
     .then(function(response) { response.json().data; }) 
     .catch(this.handleError); 
    } 

    }); 

})(window.app || (window.app = {}), window.ng); 

這裏的主要成分是:

;(function(app, ng) { 

    app.AppComponent = ng.core 
    .Component({ 
     selector: 'register-form', 
     templateUrl: 'src/register/app.component.html' 
    }) 
    .Class({ 
     constructor: [ng.core.ElementRef, app.ApplicationsService, function(ref, Applications) { 
     console.log('app.component.js'); 
     this.programs = JSON.parse(ref.nativeElement.getAttribute('programs')); 
     this.applications = Applications; 
     }], 
     emailExists: function(email) { 
     console.log('emailExists() triggered'); 
     Applications.emailExists(email); 
     } 
    }); 

})(window.app || (window.app = {}), window.ng); 

自舉:

;(function(app, ng) { 

    document.addEventListener('DOMContentLoaded', function() { 
    ng.platformBrowserDynamic.bootstrap(app.AppComponent, [ 
     ng.forms.disableDeprecatedForms(), 
     ng.forms.provideForms(), 
     ng.http.HTTP_PROVIDERS, 
     app.ApplicationsService 
    ]); 
    }); 

})(window.app || (window.app = {}), window.ng); 

如果我嘗試將http注入提供程序數組中的主要組件,它將起作用。但我寧願有一個服務。

回答

0

我發現了這個問題。看起來像Angular2需要按順序加載你的代碼。主要組件在服務之前被加載,所以它是未定義的。我把我所有的代碼放在一個文件中,它可以工作。我將盡快使用require loader。