微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-在Angular 4.2.2 CLI Web-api中找不到angular-in-memory-web-api页面

我正在学习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
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐