如何在内存web api中为两个不同的jsons

问题描述 投票:3回答:1

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

1)

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'}
        ];
        return {heroes};
    }
}      

2)

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

import {Address} from "cluster";

export class StudentData implements InMemoryDbService {
    createDb() {

        let students = [
            {
                id: 11,
                FirstName: 'Mounika',
                LastName: 'Gangamwar',
                email: '[email protected]',
                Phonenumber: 1111,
                Address: '2323',
                Password: 'asa',
                CPassword: 'aa'
            }
        ];
        return {students};
    }
}

我的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)]中添加两个数据库

我收到了StudentData服务的404错误。如果我从中删除一个dbService工作正常

我怀疑是如何为forRoot方法添加两个dbservices?

angular angular2-routing angular2-template angular2-services
1个回答
4
投票

您可以为两者使用一个服务,并使用集合的名称返回解决方案。

这是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: '[email protected]',
                Phonenumber: 1111,
                Address: '2323',
                Password: 'asa',
                CPassword: 'aa'
            }
        ];
        return {heroes, students};
    }
}

而app模块就是这样的

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 / heroes”&&“/ api / students”生成集合名称。

© www.soinside.com 2019 - 2024. All rights reserved.