简体   繁体   English

如果我在Angular2中创建新类,则会出现此错误:TypeError:http_2.Headers不是构造函数

[英]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.

相关问题 没有&#39;new&#39;就不能调用Angular2 - Class构造函数 - Angular2 - Class constructor cannot be invoked without 'new' 错误TypeError:当我使用angular2动态添加文本字段时,无法读取未定义的属性“ 0” - ERROR TypeError: Cannot read property '0' of undefined when i add text field dynamiclly with angular2 我如何访问包装可观察的angular2的构造函数的范围? - How do I access the scope of a constructor wraping an angular2 observable? 错误TypeError:如果没有在Ionic 4中使用“ new”,则无法调用类构造函数EventEmitter - ERROR TypeError: Class constructor EventEmitter cannot be invoked without 'new' in Ionic 4 角度2-我有3个标头组件。 如何最好地切换这2个标题? - Angular 2 - I have 3 headers components. Hows best to toggle these 2 headers? 尝试查看聚合物v2.0应用程序时如何解决错误“未捕获的TypeError:如果没有&#39;new&#39;,则无法调用类构造函数m” - How can I solve the error “Uncaught TypeError: Class constructor m cannot be invoked without 'new'” when trying to view a polymer v2.0 app 当我使用另一个打字稿文件中的其他类时,angular2报告错误 - angular2 reports error when I use other class which is in another typescript-file 为什么Angular中HttpClient的构造函数不需要参数,当我通过其他类的构造函数实例化它时,但需要通过new? - Why does constructor of HttpClient in Angular not need param, when I instanstiate it via constructor of other class, but needs via new? 我是否总是必须在angular2中声明变量才能获得更改? - Do I always have to declare a variable in angular2 to get changes? 我无法将参数传递给外部函数来呈现消息,TypeError:没有“new”就无法调用类构造函数 AlertMessage - I can't pass parameters to external function to render the message, TypeError: Class constructor AlertMessage cannot be invoked without 'new'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM