gpt4 book ai didi

javascript - 如果我在 Angular2 中创建新类,则会出现此错误 : TypeError: http_2. Headers is not a constructor

转载 作者:行者123 更新时间:2023-11-28 18:40:48 24 4
gpt4 key购买 nike

这是一个错误!在 v1.8 中 - 此错误已修复!

我在 TypeScript 存储库中创建问题:https://github.com/Microsoft/TypeScript/issues/7616

问题:

我创建简单的类: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;
}
}

并导入到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);
}
}

但在控制台中我有错误:TypeError: http_2.Headers is not a constructor

为什么?为什么是http_2?为什么要标题?...

当我这段代码时:

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

替换为:

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

一切正常!但我不明白为什么会出现这个错误?

现场演示:http://plnkr.co/edit/nfqjRyktqnscwUc6stbY?p=preview

最佳答案

看来你应该使用:

import {Headers} from 'angular2/http';

而不是

import {Headers} from "angular2/http";

查看这个plunkr:http://plnkr.co/edit/Eda5ypYUcn1N5XNM0FEM?p=preview .

关于javascript - 如果我在 Angular2 中创建新类,则会出现此错误 : TypeError: http_2. Headers is not a constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36134033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com