问题描述
我们如何使用内存中的角度-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.
例如,我想在内存数据库中创建两个集合-国家和城市.你知道怎么做吗?
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 }
}
如果您还不了解这一点,则{ heroes }
只是编写{ heroes: 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 }
}
返回的属性名称将用于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中的多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!