【问题标题】:Error when calling a method within service from a directive in Coffeescript从 Coffeescript 中的指令调用服务中的方法时出错
【发布时间】:2017-01-19 18:03:00
【问题描述】:

我有一个名为DashboardLayoutStyleCalculatorService 的服务,使用以下方法。其他方法已删除,因为它们与问题并不真正相关:

  styleFns =
    table: @computeStyleForTable
    single_value: @computeStyleForSingleValue

  @computeStyleFor = (type, height = null) ->
    return styleFns[type](height)

通过两个不同的指令调用此服务:

指令 1:

scope.computeStyle = (component) ->
  if component?.height?
    height = component.height * 50
  return DashboardLayoutStyleCalculator.computeStyleFor(component.element.type, height)

指令 2:

scope.computeStyle = (element, rowComponent) ->
  return DashboardLayoutStyleCalculator.computeStyleFor(element.type, rowComponent?.height?)

简而言之,我的代码所做的是,根据computeStyleFor 中的输入类型,它为表格或单值可视化调用两种不同的方法。还有一些基于某些参数的动态高度计算,这些参数对于这个问题并不完全必要。

当我在浏览器中运行此代码时,我收到以下错误:

main.webpack-a9f3967e.js:27 TypeError: e[t] is not a function
    at Object.computeStyleFor (https://localhost:54321/webpack/main.webpack-a9f3967e.js:10:17938)
    at e.t.computeStyle (https://localhost:54321/webpack/main.webpack-a9f3967e.js:10:16879)
    at fn (eval at compile (https://localhost:54321/webpack/vendor.webpack-6f544239.js:49:29700), <anonymous>:4:627)
    at d.$digest (https://localhost:54321/webpack/vendor.webpack-6f544239.js:48:11027)
    at d.$apply (https://localhost:54321/webpack/vendor.webpack-6f544239.js:48:12692)
    at https://localhost:54321/webpack/vendor.webpack-6f544239.js:47:23881 undefined

computeStyleForstyleFns 似乎存在问题。

【问题讨论】:

    标签: javascript angularjs service coffeescript directive


    【解决方案1】:

    据我所知,我会假设您的电话 component.element.type 会产生一个意外的密钥

    scope.computeStyle = (component) ->
        if component?.height?
            height = component.height * 50
        return DashboardLayoutStyleCalculator.computeStyleFor(component.element.type, height)
                                                              ^^^^^^^^^^^^^^^^^^^^^^
                                                                    |
                                                                    ---- Not what you expect
    

    因此你以后打电话给

     @computeStyleFor = (type, height = null) ->
        return styleFns[type](height)
                       ^^^^^^
                          |
                          --- fails as styleFns[type] yields undefined which is not a function
    

    添加一些输出以查看表达式 typestyleFns[type] 的产量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多