简体   繁体   English

如何在 angular 中自定义 fake-db 功能

[英]How to custom fake-db feature in angular

How to use fake-db feature (I'm using this ) in angular that support the api with response like below.如何在支持 api 的 angular 中使用 fake-db 功能(我正在使用这个),响应如下。 For getting the list获取列表

// GET 'api/outlets'
{
  data: [
    {'id':1, 'name': 'Outlet 1', ... },
    {'id':2, 'name': 'Outlet 2', ... },
    {'id':3, 'name': 'Outlet 3', ... },
  ],
}

For getting the detail获取详细信息

// GET 'api/outlets/2'
{
  data: {
    'id':2, 
    'name': 'Outlet 2', 
    ... 
  }
}

you can use json-server for that.您可以为此使用json-server you just need to create fake json whatever you want in one json file and follow the steps just like mentioned here您只需要在一个 json 文件中创建任何您想要的假 json 并按照此处提到的步骤操作

https://github.com/typicode/json-server https://github.com/typicode/json-server

See this Angular mock services stackblitz example请参阅此Angular 模拟服务 stackblitz 示例

import { Injectable } from '@angular/core';
import { customerList } from "../data/data";
import { Customer } from "../model/Customer";

@Injectable()
export class GetAllCustomersService {

  customerList: Customer[] = customerList;
  constructor() { }

  getAllCustomers() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const data = { res: this.customerList };
        resolve(data);
      }, 2000);
    });
  }
}

Found the config for in-http-memory.找到 in-http-memory 的配置。 Set dataEncapsulation to true .dataEncapsulation设置为true

HttpClientInMemoryWebApiModule.forRoot(
    InMemoryDataService, { 
        dataEncapsulation: true, 
        ...
    }
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM