Angular 角度存储参考数据

Angular 角度存储参考数据,angular,angular6,Angular,Angular6,我的Angular 6应用程序中有许多简单的查找表,形式如下: export const Fuel: { id: number, type: string } [] = [ { "id": 1, "type": "Petrol" }, { "id": 2, "type": "Diesel" }, { "id": 3, "type": "Electric" }, { "id": 4, "type": "Hybrid" } ]; 我想将所有这些存储在一个文件中,这样

我的Angular 6应用程序中有许多简单的查找表,形式如下:

export const Fuel: { id: number, type: string } [] = [
    { "id": 1, "type": "Petrol" },
    { "id": 2, "type": "Diesel" },
    { "id": 3, "type": "Electric" },
    { "id": 4, "type": "Hybrid" }
];

我想将所有这些存储在一个文件中,这样模块中的所有组件都可以访问这些查找表。post介绍如何使用环境文件存储应用程序设置。是否有一种推荐的或最佳实践方法来实现此目的?

您可以使用这些属性创建一个名为“AppSettings”的常量类

export const AppSettings: any = {

   Fuel: [
    { "id": 1, "type": "Petrol" },
    { "id": 2, "type": "Diesel" },
    { "id": 3, "type": "Electric" },
    { "id": 4, "type": "Hybrid" }
   ],

}
您可以使用在任何组件中访问

import {AppSettings} from './AppSettings.ts';

console.log(AppSettings.Fuel[0]); // { "id": 1, "type": "Petrol" }

您可以在模块外的普通ts文件中包含这些表,并且在导出这些表时,您可以在模块中使用这些表。这是使用api从服务器获取这些查找的有效方法。因此,它与客户机的依赖性较小。如果不想这样做,请创建一个模式文件,将所有查找放在其中并导入它。