我正在尝试检查调用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')
,但没有骰子。我哪里错了?
答案 0 :(得分:1)
当你打电话给mixin时,你正在进入吧台。这是一个未定义的变量。如果你要改成它。
+foo
+foo('bar')
+foo
+foo('bar')
你会发现你会得到预期的结果。