【问题标题】:Pass Angular2 Component as parameter将 Angular2 组件作为参数传递
【发布时间】:2018-01-31 11:20:57
【问题描述】:

我需要创建一个通用解决方案,通过传递一些参数从应用程序内部的任何位置打开模式,这些参数可能是大小(小、大、全屏)、可关闭(真或假)和最重要参数是我需要在此模式中显示的组件。我的问题是如何将完整组件传递给服务内部的方法。

【问题讨论】:

    标签: angular angular-components


    【解决方案1】:

    您可以简单地将组件类传递给您的服务:

    service.openModal(component = AComponent, ...rest) {}
    

    如果您想将其作为字符串传递,您可以声明将在您的服务中使用哪些组件并按名称查找它们。像这样的:

    service.dynamicComponents = [AComponent, BComponent];
    
    service.openModal(params = { component: 'AComponent' }) {
      const component = this.dynamicComponents
                            .find(o => o.constructor.name === params.component)
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 1970-01-01
      • 2011-08-12
      • 2016-09-26
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多