gpt4 book ai didi

javascript - 检查 Jade mixin 中的参数

转载 作者:行者123 更新时间:2023-12-03 10:17:35 25 4
gpt4 key购买 nike

我正在尝试检查调用 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') ,但没有骰子。我哪里出错了?

最佳答案

当你调用 mixin 并传入 bar 时。这是一个 undefined variable 。如果你要把它改成.

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

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

关于javascript - 检查 Jade mixin 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29799301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com