有条件地封装规则 [英] Conditionally encapsulate rules

查看:102
本文介绍了有条件地封装规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下内容:

@import 'hello.less'

基于变量值,我想将其嵌套在另一个类中.以下是我要完成的 pseudocode

Based on a variable value, I want to nest this inside another class. The following is what I want to accomplish, in pseudocode:

if(@someVar = true):
  .someClass {
    @import 'hello.less'
  }
else:
  @import 'hello.less'

我想我可以使用混合守卫,但我不知道如何处理其他"情况:

I guess I could use mixin guards, but I don't know how to handle the 'else' case:

.someClass when (@someVar = true) {
  @import 'hello.less'
}

// Else?

推荐答案

使用 & (&展开

@some-var: true;

.some-class when (@some-var) {
    @import (multiple) "hello";
}

& when not(@some-var) {
    @import (multiple) "hello";
}

请注意,在这种情况下,必须同时使用 multiple 导入选项始终会进行评估,并且防护人员仅控制内容是否出现在输出中.

Note that multiple import option is required in this case since both imports are always evaluated and the guards only control if the content appears in output or not.

---

尽管总的来说,我想知道为什么您需要将不同项目的代码保存在同一文件中,并通过变量间接控制其编译.相反,我建议您仅创建命名间隔的问候"文件:

Though in general I wonder why you need to keep the code for different projects in the same file and control its compilation indirectly via variables. Instead I'd suggest to create just "namespaced hello" file:

.some-class {@import "hello";}

并将其无条件地导入您想要的项目中.

and import it unconditionally in the project you want it that way.

这篇关于有条件地封装规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆