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

标签 javascript angular npm

我正在学习 Angular 框架(使用 4.2.2 版)并遵循英雄之旅教程。在 HTTP 部分,教程使用 angular-in-memory-web-api 来模拟 web api 服务器,我尝试使用它但它不起作用。来自浏览器控制台的消息告诉我 响应状态:404 未找到 URL:api/heroes 我通过 node install npm i 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 数组绑定(bind)到对象中的“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
  }
}

关于javascript - 在 Angular 4.2.2 CLI web-api 中找不到 angular-in-memory-web-api 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44592830/

相关文章:

maven - 将 lib maven 导入 npm package.json

javascript - 使用 JS 在 webdriver 中设置下载目录

javascript - D3 和​​音阶中的 Colorbrewer

angular - 如何在 Angular 组件中使用枚举

asp.net - 在 Angular 中使用 Swagger

reactjs - Webpack 命令似乎无法在 Windows CLI 上运行

javascript - JS 中的简单数学加法给出错误结果

javascript - 我如何编辑这个 fiddle 以在几秒钟后关闭动画?

javascript - Angular 9 : How show with a HttpInterceptor the stacktrace of a request?

node.js - 安装 Node.JS 永久 NPM 包时,在 git 上出错 ://address dependency