为什么api结果数组由data []组成?

时间:2018-11-27 05:04:29

标签: angular6

我使用了以下代码。它可以正常工作,但我担心api为何会以“ data:”结果出现,如下所示

data: Array(2)
0: {id: 1, title: "Home", link: "/home"}
1: {id: 2, title: "About", link: "/about"}

我一直在学习本教程,它给出的输出类似

(2)[{..},{..}]

我不是同一个代码给我不同的输出。有人可以帮我吗?

谢谢。

in-memory-data-service.ts

export class InMemoryDataService implements InMemoryDbService {

    constructor() { }

    createDb() {
        const menu = [
            { id: 1, title: 'Home', link: '/home' },
            { id: 2, title: 'About', link: '/about' }
        ];
        return { menu };
    }

navigation.component

导出类NavigationComponent实现OnInit {

menu: any;
database = 'menu';

constructor(private auth: AuthenticationService, private config: ConfigService) { }

ngOnInit() {
    this.getMenu(this.database);
}

getMenu(database) {
    this.config.getSettings(database).subscribe(
        res => { 
            this.menu = res;
        }
    );
}

config.service

getSettings(database: string, id?: any): Observable<any[]> {
    let uid = id || null;
    let url: string;
    if (uid !== null) {
        url = `api/${database}/${id}`;
    } else {
        url = `api/${database}`;
    }
    return this.http.get<any>(url);
}

0 个答案:

没有答案