2016-10-04 115 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, ] 
}) 
相關問題