mixin のすべての引数を含む変数を作成し、その変数を mixin に渡したいと思います。これは可能ですか?もしそうなら、なぜこれがうまくいかないのですか?
- var foo = "'arg1','arg2','arg3'"
mixin bar(arg1, arg2, arg3)
div
#{arg1}, #{arg2}, #{arg3}
+bar(foo)
mixin のすべての引数を含む変数を作成し、その変数を mixin に渡したいと思います。これは可能ですか?もしそうなら、なぜこれがうまくいかないのですか?
- var foo = "'arg1','arg2','arg3'"
mixin bar(arg1, arg2, arg3)
div
#{arg1}, #{arg2}, #{arg3}
+bar(foo)
What you're trying does not work because you're using a string as single argument and invoke your bar mixin with a single string. You don't split the string arguments at the ',' character so only your arg1 contains a value.
First let's repair your bar mixin
mixin bar(args)
div
each arg in args
| #{arg}
The code uses only one argument named 'args'. For this argument some enumerable or array like type is expected. See jade documentation for more information about iterations. The code block | #{arg}
writes the arg value as plain text.
How to invoke this mixin? Instead of providing an argument as string you'll need to provide an argument array:
mixin bar(['arg1','arg2','arg3'])
In case you want to pass a string as argument you'd have to rewrite your mixin bar:
mixin barString(argString)
- args = argString.split(',');
div
each arg in args
| #{arg}
Then you could call the barString mixin:
mixin barString("arg1,arg2,arg3")
Note that I've removed the single quotes for this call.
これは遅いです、あなたはこのようにすることもできます
mixin bar(...args)
each arg in args
| #{arg}
+bar('arg1', 'arg2', 'arg3')