【问题标题】:Angular storing reference data角度存储参考数据
【发布时间】:2019-01-29 16:58:40
【问题描述】:

我的 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" }
];

我想将所有这些存储在一个文件中,以便模块内的所有组件都可以访问这些查找表。 This 帖子描述了使用环境文件来存储应用程序设置。是否有推荐或最佳实践方法来实现这一目标?

【问题讨论】:

  • 您可以在模块外的普通ts文件中拥有这些表,当这些表将被导出时,您可以在模块中使用。
  • 使用 api 从服务器获取这些查找的有效方法。所以它对客户端的依赖会更少。如果您不想这样做,请创建一个模态文件,将所有查找放入其中并导入。

标签: angular angular6


【解决方案1】:

您可以使用这些属性创建一个名为“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" }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-31
    • 2015-06-23
    • 1970-01-01
    • 2021-07-20
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多