Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2:其中是url';api/heroes';定义_Angular_Angular2 Routing - Fatal编程技术网

Angular 角度2:其中是url';api/heroes';定义

Angular 角度2:其中是url';api/heroes';定义,angular,angular2-routing,Angular,Angular2 Routing,我正在学习教程的第7部分。在MemoryWebAPI模块中包括后,hero.service.ts使用private-heroesUrl='api/heroes' 应用程序如何知道WebAPI url是api/heroes?我在app routing.module.ts中没有看到这一定义。到InMemoryWebAPI模块的url映射是如何工作的 我将继续我在这里的经历。。。对InMemoryWebAPI模块没有深入了解 似乎“api”是由InMemoryWebApi捕获的。“英雄”是create

我正在学习教程的第7部分。在MemoryWebAPI模块中包括后,
hero.service.ts
使用
private-heroesUrl='api/heroes'


应用程序如何知道WebAPI url是
api/heroes
?我在
app routing.module.ts
中没有看到这一定义。到InMemoryWebAPI模块的url映射是如何工作的

我将继续我在这里的经历。。。对InMemoryWebAPI模块没有深入了解

似乎“api”是由InMemoryWebApi捕获的。“英雄”是createDb()方法中定义的数据结构

例如,我的如下所示:

private baseUrl = 'api/products';
export class ProductData implements InMemoryDbService, InMemoryBackendConfig {
    createDb() {
        let products: IProduct[] = [ ...];
        return { products };
    }
}
我的数据文件如下所示:

private baseUrl = 'api/products';
export class ProductData implements InMemoryDbService, InMemoryBackendConfig {
    createDb() {
        let products: IProduct[] = [ ...];
        return { products };
    }
}

在该教程的页面顶部附近,他们定义了此项的导入

--删除了主题外的代码参考,我有点误读了这个问题--

然后,在下面,他们给了我们这个项目的代码

InMemoryWebAPI使用 要创建一个本地数据库,对它的调用将引用。此模块似乎是捕获/注册/api调用的项


这只是使用实际后端的一种替代方法。

InMemoryWebApi是一种固执己见的工具,它使用其内部约定

createDb
方法应该返回一个对象,其中键(即您附加数据的位置)将匹配api URL的slug,因此为了处理
英雄的slug
,请返回
{heros:yourData}
。通常,在以下情况下使用速记:

const heroes = [ /* your data */ ];
return { heroes }; 
现在,
api
是这个库假定的默认前缀。它可以根据您的具体情况进行更改,只需将其作为
InMemoryBackendConfigArgs
参数的一部分提供即可。例如,如果您知道您的英雄端点将位于
myapi/v2/heromes
,请使用以下配置:

HttpClientInMemoryWebApiModule.forRoot(DataInmemService, {
  apiBase: 'myapi/v2'
})

调整apiBase参数解决了我的问题,我花了4个小时试图理解为什么我不能获得假数据。