2016-10-04 54 views
7

我是angular2和typescript中的新成员,已经花了半天的时间来弄清楚ng2表单。我已经完成了我所有的路线和建造所有必要的形式,目前想了解如何在angular2发布与打字稿属性'http'在类型'Component'上不存在,Angular 2

这是我的错误:

ERROR in [default] simpleapp/src/app/clients/add-client/add-client.component.ts:52:16 Property 'http' does not exist on type 'AddClientComponent'.

而且我找不到在哪里是这个问题的来源。我在我的组件中导入了angular/http,我提供了标题和响应作为官方教程说但仍然可以看到这个问题。我错过了什么,我的问题在哪里?在此先感谢

这是我组件

import 'rxjs/add/operator/map'; 

import {Component} from '@angular/core'; 
import {Http, Response, RequestOptions, Headers} from '@angular/http'; 

import {Employee} from "./model/add-client.model"; 

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

export class AddClientComponent { 

    departments: Array<any>; 
    firstName: ''; 
    lastName: ''; 
    id: null; 
    salary: null; 
    phone: null; 
    departmentId: null; 

    constructor(http: Http) { 
    http.get('api/departments') 
     .map((res: Response) => res.json()) 
     .subscribe((departments: Array<any>) => this.departments = departments); 
    } 

    model = new Employee(
    this.id, 
    this.firstName, 
    this.lastName, 
    this.salary, 
    this.departmentId, 
    this.phone 
); 

    submitted = false; 

    addEmployee = 'api/employees' 

    handleError = 'Post Error'; 

    onSubmit(model) { 
    this.submitted = true; 

    let body = JSON.stringify({ model }); 
    let headers = new Headers({ 'Content-Type': 'application/json' }); 
    let options = new RequestOptions({ headers: headers }); 

    return this.http.post(this.addEmployee, body, options) 
     .catch(this.handleError); 

    } 
} 

这是我模板

<div class="container"> 
    <md-card class="demo-card demo-basic"> 
     <md-card-title color="primary back-header">Employee Form</md-card-title> 
     <md-card-content> 
     <form (ngSubmit)="onSubmit(model)" #employeeForm="ngForm"> 
      <md-toolbar for="firstName">First Name</md-toolbar> 
      <md-input 
      class="demo-full-width input-text" 
      type="text" 
      id="firstName" 
      required 
      [(ngModel)]="model.firstName" 
      name="firstName" 
      #firstName="ngModel"> 
      </md-input> 

      <md-toolbar for="lastName">Last Name</md-toolbar> 
      <md-input 
      class="demo-full-width input-text" 
      type="text" 
      id="lastName" 
      required 
      [(ngModel)]="model.lastName" 
      name="lastName" 
      #lastName="ngModel"> 
      </md-input> 

      <md-toolbar for="salary">Salary</md-toolbar> 
      <md-input 
      class="demo-full-width input-text" 
      type="number" 
      id="salary" 
      placeholder="USD" 
      required 
      [(ngModel)]="model.salary" 
      name="salary" 
      #salary="ngModel"> 
      </md-input> 

      <md-toolbar for="departmentId">Department</md-toolbar> 
      <select class="demo-full-width option-department input-text" 
        id="departmentId" 
        required 
        [(ngModel)]="model.departmentId" 
        name="departmentId" 
        #departmentId="ngModel"> 
       <option 
       *ngFor="let department of departments" 
       [value]="department.id">{{department.name}} 
       </option> 
      </select> 

      <md-toolbar for="phone">Phone</md-toolbar> 
      <md-input 
      class="demo-full-width input-text" 
      type="number" 
      id="phone" 
      placeholder="phone #" 
      required 
      [(ngModel)]="model.phone" 
      name="phone" 
      #phone="ngModel"> 
      </md-input> 

      <button md-raised-button color="primary" 
        type="submit" 
        class="btn btn-default" 
        [disabled]="!employeeForm.form.valid">Submit 
      </button> 
     </form> 
     </md-card-content> 
    </md-card> 
    <md-card [hidden]="!submitted"> 
     <md-card-title>You submitted the following:</md-card-title> 
    <md-list> 
     <md-list-item> 
     <label>First Name:</label> {{model.firstName}} 
     </md-list-item> 
     <md-list-item> 
     <label>Last Name:</label> {{model.lastName}} 
     </md-list-item> 
     <md-list-item> 
     <label>Salary:</label> {{model.salary}} 
     </md-list-item> 
     <md-list-item> 
     <label>Department:</label> {{model.departmentId}} 
     </md-list-item> 
     <md-list-item> 
     <label>Phone:</label> {{model.phone}} 
     </md-list-item> 
    </md-list> 
    </md-card> 
</div> 

这是我模块

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 


import { MaterialModule } from '@angular/material'; 
import {MdListModule} from '@angular/material/list'; 


import { AppComponent } from './app.component'; 
import { routing, appRoutingProviders } from './app.routing'; 

//============== 

import { ClientsComponent } from './clients/clients.component'; 
import { DepartmentsComponent } from './departments/departments.component'; 
import { AddClientComponent } from './clients/add-client/add-client.component'; 
import { AddDepartmentComponent } from './departments/add-department/add-department.component'; 

@NgModule({ 

    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule, 
    routing, 

    MaterialModule.forRoot(), 
    MdListModule.forRoot() 
    ], 

    declarations: [ 
    AppComponent, 
    ClientsComponent, 
    DepartmentsComponent, 
    AddClientComponent, 
    AddDepartmentComponent 
    ], 

    providers: [appRoutingProviders], 

    bootstrap: [AppComponent] 
}) 
export class AppModule { } 

回答

14

只需添加private,让您的Http实例提供给您的整个组件:

constructor(private http: Http) 
+1

哦,它的工作原理,谢谢!你能解释魔法在哪里吗? – antonyboom

+0

@antonyboom很高兴我能帮到你。在以下链接上有非常好的答案,我不认为我可以给你比他们任何人更好的解释:http://stackoverflow.com/questions/36147890/angular2-what-is-different-between-the-变量声明在类和和http://stackoverflow.com/questions/34574167/angular2-should-private-variables-be-accessible-in-the-template –

+1

再次感谢你! – antonyboom

0

它是与你的http变量试试这个

在你component.ts

constructor(http: Http) { 
    http.get('api/departments') 
     .map((res: Response) => res.json()) 
     .subscribe((departments: Array<any>) => this.departments = departments); 
    } 

你可以尝试

constructor(private http: Http) { 
    http.get('api/departments') 
     .map((res: Response) => res.json()) 
     .subscribe((departments: Array<any>) => this.departments = departments); 
    } 
+0

'http'对构造函数是本地的。 – Igor

0

您必须在导出声明中导出模块中的Http模块。

@NgModule({ 
    imports: [CommonModule, RouterModule, ReactiveFormsModule, ], 
    declarations: [ ErrorMessagesComponent, FoodDashboardComponent ], 
    exports: [ CommonModule, ReactiveFormsModule, HttpModule, RouterModule, ] 
}) 
相关问题