我正在学习Angular框架(使用4.2.2版),并遵循教程“英雄之旅”.在HTTP部分中,该教程使用angular-in-memory-web-api来模拟Web api服务器,我尝试使用它,但是它不起作用.来自浏览器控制台的消息告诉我
状态为:404找不到URL:api / heroes的响应
我通过节点安装npm安装了angular-in-memory-web-api,我安装了angular-in-memory-web-api,这是我的代码(与教程相同)implement in-memory-web-api for api/heroes import module我从项目中错过了什么.
解决方法:
检查app / in-memory-data.service.ts,其中createDb()方法返回api端点.返回的对象使用ES6语法“对象解构”,该结构将heroes数组绑定到对象中的“ heroes”字段.
import { InMemoryDbService } from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
createDb() {
const heroes = [
{ id: 0, name: 'Zero' },
{ id: 11, name: 'Mr. Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
return {heroes}; // !!!pay attention to this line; it creates api/heroes rest endpoint
}
}