Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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 如何为两个不同的JSON创建内存中的web api_Angular_Angular2 Routing_Angular2 Template_Angular2 Services - Fatal编程技术网

Angular 如何为两个不同的JSON创建内存中的web api

Angular 如何为两个不同的JSON创建内存中的web api,angular,angular2-routing,angular2-template,angular2-services,Angular,Angular2 Routing,Angular2 Template,Angular2 Services,我正在为两个json服务(DB)使用内存中的web api (一) (二) 我的app.module.js是 import {InMemoryWebApiModule} from 'angular-in-memory-web-api'; import {InMemoryDataService} from './in-memory-data.service'; import {StudentData} from './forms/student.data.service'; @ NgMod

我正在为两个json服务(DB)使用内存中的web api

(一)

(二)

我的app.module.js是

import {InMemoryWebApiModule} from 'angular-in-memory-web-api';

import {InMemoryDataService}  from './in-memory-data.service';

import {StudentData} from './forms/student.data.service';

@
NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        InMemoryWebApiModule.forRoot(InMemoryDataService, StudentData)
    ],
    declarations: [AppComponent],
    bootstrap: [UIView]
})
我的问题是我无法在
InMemoryWebApiModule.forRoot(InMemoryDataService,StudentData)]

学生数据服务出现404错误。如果我从中删除一个dbService,它就可以正常工作


我的疑问是如何向forRoot方法中添加两个DBService?

您可以对这两个方法都使用一个服务,并返回两个服务以及解决方案集合的名称

这是InMemoryDataService

import {InMemoryDbService} from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
    createDb() {
        let heroes = [
            {id: 11, name: 'Mr. Nice'},
            {id: 12, name: 'Narco'},
            {id: 13, name: 'Bombasto'},
            {id: 14, name: 'Celeritas'},
            {id: 15, name: 'Magneta'},
            {id: 16, name: 'RubberMan'},
            {id: 17, name: 'Dynama'},
            {id: 18, name: 'Dr IQ'},
            {id: 19, name: 'Magma'},
            {id: 20, name: 'Tornado'}
        ];
        let students = [
            {
                id: 11,
                FirstName: 'Mounika',
                LastName: 'Gangamwar',
                email: 'asa@gmailc.com',
                Phonenumber: 1111,
                Address: '2323',
                Password: 'asa',
                CPassword: 'aa'
            }
        ];
        return {heroes, students};
    }
}
应用程序模块就是这样

import {InMemoryWebApiModule} from 'angular-in-memory-web-api';
import {InMemoryDataService}  from './in-memory-data.service';
@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        InMemoryWebApiModule.forRoot(InMemoryDataService, {apiBase: '/api'})
    ],
    declarations: [AppComponent],
    bootstrap: [UIView]
})

集合名称将根据每个学生和英雄的URL生成“/api/herones”和“/api/students”。

您找到解决方案了吗?解决方案已经可用并在其repo()中关闭了。有没有办法从db.json文件中读取英雄和学生的数据?
import {InMemoryDbService} from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
    createDb() {
        let heroes = [
            {id: 11, name: 'Mr. Nice'},
            {id: 12, name: 'Narco'},
            {id: 13, name: 'Bombasto'},
            {id: 14, name: 'Celeritas'},
            {id: 15, name: 'Magneta'},
            {id: 16, name: 'RubberMan'},
            {id: 17, name: 'Dynama'},
            {id: 18, name: 'Dr IQ'},
            {id: 19, name: 'Magma'},
            {id: 20, name: 'Tornado'}
        ];
        let students = [
            {
                id: 11,
                FirstName: 'Mounika',
                LastName: 'Gangamwar',
                email: 'asa@gmailc.com',
                Phonenumber: 1111,
                Address: '2323',
                Password: 'asa',
                CPassword: 'aa'
            }
        ];
        return {heroes, students};
    }
}
import {InMemoryWebApiModule} from 'angular-in-memory-web-api';
import {InMemoryDataService}  from './in-memory-data.service';
@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        InMemoryWebApiModule.forRoot(InMemoryDataService, {apiBase: '/api'})
    ],
    declarations: [AppComponent],
    bootstrap: [UIView]
})