4

mixin のすべての引数を含む変数を作成し、その変数を mixin に渡したいと思います。これは可能ですか?もしそうなら、なぜこれがうまくいかないのですか?

- var foo = "'arg1','arg2','arg3'"

mixin bar(arg1, arg2, arg3)
  div 
    #{arg1}, #{arg2}, #{arg3}

+bar(foo)
4

2 に答える 2

6

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.

于 2013-03-04T07:23:30.677 に答える
0

これは遅いです、あなたはこのようにすることもできます

mixin bar(...args)
    each arg in args
        | #{arg}

+bar('arg1', 'arg2', 'arg3')
于 2016-09-16T07:57:01.917 に答える