在与Heroes AngularJS教程一起编码时,为什么会出现HTTP 404错误?

时间:2019-03-26 13:58:42

标签: angularjs angularjs-http

我是AngularJS的新手,并尝试使用Angular Heroes Tutorial。到目前为止,一切顺利,直到我点击了HTTP教程(https://angular.io/tutorial/toh-pt6

我尝试重新排序app.module.ts导入声明数组。我尝试卸载-重新安装in-memory-web-api。我检查了InMemoryDataService返回的收藏集,但到目前为止还没有运气。想知道我是否能克服它。

App.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { EmployeesComponent } from './employees/employees.component';
import { FormsModule } from '@angular/forms';
import { EmployeeDetailComponent } from './employee-detail/employee-
detail.component';
import { MessagesComponent } from './messages/messages.component';
import { DashboardComponent } from './dashboard/dashboard.component'; // <-- 
NgModel lives here
import { HttpClientModule }    from '@angular/common/http';
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService }  from './in-memory-data.service';

@NgModule({
  declarations: [
    AppComponent,
    EmployeesComponent,
    EmployeeDetailComponent,
    MessagesComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    HttpClientInMemoryWebApiModule.forRoot(
      InMemoryDataService, { dataEncapsulation: false }),
    AppRoutingModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }

employee.service.ts

  getEmployee(ecode: number): Observable<Employee> {
    const url = `${this.employeesUrl}/${ecode}`;
    return this.http.get<Employee>(url).pipe(
      tap(_ => this.log('fetched employee ecode=${ecode}')),
      catchError(this.handleError<Employee>(`getEmployee ecode=${ecode}`))
    );
  }

  /** PUT: update the employee on the server */

  updateEmployee(employee: Employee): Observable<any> {
    return this.http.put(this.employeesUrl, employee, httpOptions)
      .pipe(
        tap(_ => this.log(`updated employee ecode=${employee.ecode}`)),
        catchError(this.handleError<any>('updateEmployee'))
      );
  }
}

我希望getEmployees()方法有效。但是,出现以下错误:

body: {error: "'employees' with id='13' not found"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
status: 404
statusText: "Not Found"
url: "api/employees/13"

(通过开发者工具控制台)

0 个答案:

没有答案