如何根据变量导入LESS样式表?

时间:2014-11-11 10:55:41

标签: less

@template-productlist-id = 1;

   & when (@template-productlist-id = 0) {
      import "product-lists/product-list0.less";
   }

   & when (@template-productlist-id > 0) {
      import "product-lists/product-list.less";
   }

但它似乎并没有起作用。仍然导入2个文件。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

您的代码无效,请尝试:

  1. 您应该使用冒号(:
  2. 分配变量
  3. 您的import语句应以at(@
  4. 开头

    @template-productlist-id: 0;
    .import() {
       & when (@template-productlist-id = 0) {
          @import "product-lists/product-list0.less";
       }
    
       & when (@template-productlist-id > 0) {
          @import "product-lists/product-list.less";
       }
    }
    .import();
    

    或者下面的Less代码也会给出预期的结果:

       @template-productlist-id: 0;
       & when (@template-productlist-id = 0) {
          @import "product-lists/product-list0.less";
       }
    
       & when (@template-productlist-id > 0) {
          @import "product-lists/product-list.less";
       }