从Es5中的Angular2 http获取错误

时间:2016-08-05 02:00:11

标签: http angular ecmascript-5

我正在尝试使用Angular2的http。 这是我的代码:

  var _domain = 'http://localhost:3000/';

   app.Applications = ng.core.Injectable().Class({
      constructor: [ng.http.Http, function(http) {
        this.http = http;
        this.emailExistUrl = _domain + 'api/applications/email';
      }],

      doesEmailExist: function(email) {
        var data = { email: email };
        return this.http.post(this.emailExistUrl, data)
          .toPromise()
          .then(function(response) { response.json().data; })
          .catch(this.handleError);
      }
    });

主要组成部分:

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

这是main.js文件:

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

当调用doEmailExist时,我从http模块收到错误:

vendor-client.min.js:55470 TypeError:无法读取未定义的属性“ platform_browser_private

有什么想法吗?

FIXED: Http在脚本标记列表上的平台浏览器之前。 :/

<script src="https://npmcdn.com/@angular/http/bundles/http.umd.js"></script>
<script src="https://npmcdn.com/@angular/platform-browser/bundles/platform-browser.umd.js"></script>
<script src="https://npmcdn.com/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js"></script>

逆转更好:)

1 个答案:

答案 0 :(得分:1)

尝试在构造函数的开头指定http

   app.Applications = ng.core.Injectable().Class({
      constructor: [ng.http.Http, function(http) {
        this.http = http;
        ...
      }],

      doesEmailExist: function(email) {
        ...
      }
    });

EDIT 见此Plunker:http://plnkr.co/edit/aQWqxauklT7MqSjfhLFD。为简化起见,我已将所有内容放在main.js文件中,而不是http帖子,我已实现了http get。但是,在本地,即使是http帖子也可以使用Web服务API。我希望它有助于解决您的问题。

相关问题