weixin_36816337
为了寻找2B
采纳率88.9%
2017-12-25 16:30

Unexpected value 'HttpHeaders'

5
已采纳

angularjs新手一个,在用http.post的时候 出现了这个问题。。。
Unexpected value 'HttpHeaders' imported by the module 'AppModule'. Please add a @NgModule annotation.
syntaxError @ compiler.js:485
(anonymous) @ compiler.js:15225
CompileMetadataResolver.getNgModuleMetadata @ compiler.js:15200
JitCompiler._loadModules @ compiler.js:34255
JitCompiler._compileModuleAndComponents @ compiler.js:34216
JitCompiler.compileModuleAsync @ compiler.js:34110
CompilerImpl.compileModuleAsync @ platform-browser-dynamic.js:230
PlatformRef.bootstrapModule @ core.js:5568
(anonymous) @ main.ts:11
../../../../../src/main.ts @ main.bundle.js:52
webpack_require @ inline.bundle.js:55
0 @ main.bundle.js:67
webpack_require @ inline.bundle.js:55
webpackJsonpCallback @ inline.bundle.js:26
(anonymous) @ main.bundle.js:1

我的代码如下:
app.component.ts

 import { Component } from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
import { OnInit } from '@angular/core/src/metadata/lifecycle_hooks';
import { HttpClient } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent   {
  title = '用户登录';
  results: string;
  Account : string;
  Password : string;
  ifms :string; 

 constructor(private http:HttpClient){}

  getdata(): void { 
    this.http.get('http://localhost:9000/login').subscribe(data =>{
      this.results = data['name'];
   })
  }
  postdata(): void {
    const body = {name: 'Brad'};
    this.http
  .post('/api/items/add', body, {
    headers: new HttpHeaders().set('Authorization', 'my-auth-token'),
  })
  .subscribe();
  }

}

app.module.ts

 import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule }   from '@angular/forms';
import { AppComponent } from './app.component';
import {HttpClientModule} from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
@NgModule({
  declarations: [
    AppComponent,

  ],
  imports: [
    BrowserModule,
    FormsModule,    
    HttpClientModule,
    HttpHeaders
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我觉得我这个HttpHeaders导入没有问题啊,怎么会这样。。希望哪位大佬可以解释一下,谢谢了!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • weixin_36816337 为了寻找2B 4年前

    最后自己找到解决方法了,原来要把

     imports: [
        BrowserModule,
        FormsModule,    
        HttpClientModule,
        HttpHeaders
      ],
    

    里面的 HttpHeaders 删掉。

    点赞 评论 复制链接分享
  • dabocaiqq dabocaiqq 4年前

    HttpHeaders不是预期的值

    点赞 1 评论 复制链接分享