简体   繁体   中英

if I create new class in Angular2 I have this error: TypeError: http_2.Headers is not a constructor

This is a bug! In v1.8 - this bug is fixed!

I create issue in TypeScript repository: https://github.com/Microsoft/TypeScript/issues/7616

Problem:

I create simple class: defs.ts

export class default_config {
    token:string
}

export var HEROES:default_config = {
    token: 'a'
};

export class HeroService {
    getHeroes() {
        return HEROES;
    }

    setToken(key) {
        HEROES.token = key;
    }
}

And import to app.ts:

//our root app component
import {Component} from 'angular2/core'
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {RouteConfig, ROUTER_DIRECTIVES} from "angular2/router";
import {Headers} from "angular2/http";
import {HeroService} from "./defs";

@Component({
  selector: 'my-app',
  providers: [HTTP_PROVIDERS, HeroService],
  template: `
    <div>
      <h2>Hello1 {{name}}</h2>
      <br>
      <router-outlet></router-outlet>
    </div>
  `
  directives: [ROUTER_DIRECTIVES]
})

export class App {
  constructor(private http:Http, serv: HeroService) {
    this.name = 'Angular2';
    var heads = new Headers();
    console.log(heads);
  }
}

But in console I have error: TypeError: http_2.Headers is not a constructor

Why? Why http_2? Why headers?...

When I this code:

import {Headers} from "angular2/http";
import {HeroService} from "./defs";

replace to this:

import {HeroService} from "./defs";
import {Headers} from "angular2/http";

all working fine! But I can not understand why I have this error?

Live demo: http://plnkr.co/edit/nfqjRyktqnscwUc6stbY?p=preview

It seems that you should use:

import {Headers} from 'angular2/http';

instead of

import {Headers} from "angular2/http";

See this plunkr: http://plnkr.co/edit/Eda5ypYUcn1N5XNM0FEM?p=preview .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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