less.js guards and conditionals

末鹿安然 提交于 2019-12-11 12:15:57

问题


I have the following less mixin:

@myColor = #123456;

.mixin(@a) when (@a = @myColor){ // do something }

This however throws this error: Unable to perform comparison

Why is that?


回答1:


It would appear (as of lesscss 1.2.2) that guards only support comparing Dimensions and Keywords. (search the source for "compare:") So, sadly, comparing two colors or strings just won't work.




回答2:


UPDATE: Four thoughts (again, I don't have experience with LESS itself, I'm just looking at documentation).

  1. Is the variable you are passing to the mixin (the @a) a color in hex format? Perhaps it cannot do the comparison because you are passing a different variable type.

  2. Just to be sure, did you double check that you called the right variable (@myColor) not (@mycolor).

  3. Have you tried reversing the when to (@myColor = @a) to see if that works. On the one example at http://lesscss.org/ where they use a variable not defined in the mixin (they use @media) that is the order they have it in.

  4. Finally, my first original answer was to try:

    .mixin(@a, @b: @myColor) when (@a = @b){ // do something }




回答3:


Just update less.js to the last version (1.2.2 - current) and it will work.

I had the same problem. Now it fixed for me (My question with code example: Lesscss Mixins with guards. Syntax Error ). I think, it will be fixed and for your example.



来源:https://stackoverflow.com/questions/9491716/less-js-guards-and-conditionals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!