Angular-in-memory-web-api 中的多个集合 [英] Multiple collections in Angular-in-memory-web-api

查看:26
本文介绍了Angular-in-memory-web-api 中的多个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用 Angular-in-memory-web-api 创建多个集合?单一收藏不是问题.但我无法为多个集合实现它.

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 for multiple collections.

例如,我想在内存数据库中创建两个集合 - Country 和 Cities.知道怎么做吗?

For example, I want to create say two collections in the memory db - Country and Cities. Any idea, how to do that?

推荐答案

只需返回一个包含两个数组的对象即可.在 Angular 的示例中,您会看到类似

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

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

如果您还不知道这一点,{ hero} 只是编写 { hero: hero} 的简写.因此,如果您有两个集合,则只需将其添加为另一个属性

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 }
}

返回的属性名称将用于 URL 中的路径.所以你可以使用

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

这篇关于Angular-in-memory-web-api 中的多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆