[英]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! 在v1.8中-此错误已修复!
I create issue in TypeScript repository: https://github.com/Microsoft/TypeScript/issues/7616 我在TypeScript存储库中创建问题: https : //github.com/Microsoft/TypeScript/issues/7616
Problem: 问题:
I create simple class: defs.ts 我创建一个简单的类: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: 并导入到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 但是在控制台中,我出现错误:TypeError:http_2.Headers不是构造函数
Why? 为什么? Why http_2? 为什么是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 现场演示: 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 . 请参阅以下代码: http ://plnkr.co/edit/Eda5ypYUcn1N5XNM0FEM?p=preview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.