从 Coffeescript 中的指令调用服务中的方法时出错

Error when calling a method within service from a directive in Coffeescript

我有一个名为 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?)

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

当我在浏览器中 运行 这段代码时,出现以下错误:

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 似乎有问题。

据我所知,我会假设您的调用 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] 产生的结果