Multiple collections in Angular-in-memory-web-api

前端 未结 3 1485
攒了一身酷
攒了一身酷 2021-02-01 13:28

How do we create multiple collections using Angular-in-memory-web-api? Not an issue with single collection. But I\'m not able to implement it f

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 14:13

    Just return it an object with both arrays. In the example from Angular, you see something like

    createDb() {
      let heroes = [ .. ]
      return { heroes }
    }
    

    If you don't already know this, { heroes } is just shorthand for writing { heroes: heroes }. So if you have two collections, then just add it as a another property

    createDb() {
      let heroes = [ .. ];
      let crises = [ .. ];
      return { heroes, crises };
      // or { heroes: heroes, crises: crises }
    }
    

    The name of the property returned will be used for the path in the URL. So you can use

    /api/heroes/1
    /api/crises/1
    

提交回复
热议问题