【问题标题】:Angular common function should put module or parent componentAngular常用函数应该放模块或者父组件
【发布时间】:2019-08-11 01:45:55
【问题描述】:

我现在的项目是这样的。 Test.component 是有两个子组件的父组件

然后两个子组件都加载一个子多子组件

问题是

我的 child-1、child-2、child-more 组件

有很多常用的函数和变量

我应该创建一个share.module并将常用的函数和变量放在这里。

或者只是放入父组件(test.component)

哪种方法更好?

或者有没有更好的方法?

【问题讨论】:

  • 创建一个它们都可能扩展的抽象基类?
  • 或者只是带有导出函数的 .ts 文件,然后您可以将其导入到您想要的任何组件文件中。公共变量是另一回事,这可能最好使用公共基类来解决。但是,如果您的意思是在组件实例之间共享值,那么您应该使用这些值创建一个服务。
  • 另一种方法是为通用功能创建服务。

标签: angular


【解决方案1】:

我应该创建一个 share.module 并将常用功能和 变量在这里。

如果您想创建一个保留公共变量并使用这些变量的类,以下示例可能会给您一个想法。 https://angular-c8shbr.stackblitz.io/ 和编辑链接:https://stackblitz.com/edit/angular-c8shbr

export class General
{
  public static listCountry={"90":"Turkey","91":"Other Country"};
  

  public static getCountryByFilter(field)
  {
    return this.listCountry[field];
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2020-12-22
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2017-12-03
    相关资源
    最近更新 更多