【问题标题】:Checking for parameters in Jade mixin在 Jade mixin 中检查参数
【发布时间】:2015-06-30 05:07:23
【问题描述】:

我正在尝试检查调用 Jade mixin 时是否设置了参数。以下示例不起作用,我不明白为什么。

mixin foo(bar)

  if bar
    - var text = 'a'
  else
    - var text = 'another'

  .hero-unit.macro-unit
    p This is #{text} mixin.


// Calling mixin:

+foo
+foo(bar)
+foo
+foo(bar)

预期输出:

This is a mixin.
This is another mixin.
This is a mixin.
This is another mixin.

实际输出:

This is a mixin.
This is a mixin.
This is a mixin.
This is a mixin.

我也尝试按照this answer 中的建议将if bar 更改为if (typeof(username) !== 'undefined'),但没有骰子。我哪里错了?

【问题讨论】:

    标签: javascript node.js templates pug gulp


    【解决方案1】:

    当你调用 mixin 并且传入 bar.这是一个未定义的变量。如果你要改成。

    +foo
    +foo('bar')
    +foo
    +foo('bar')
    

    你会发现你会得到预期的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      相关资源
      最近更新 更多