LESS variable file not global

2019-07-31 13:24发布

问题:

I use less-loader with webpack, the annoying part is I have to import the variable.less in every single of my module's less file. Why it can't be just global?

回答1:

It's not clear from your question (can't tell if the problem you're having is something about your webpack setup), but if you're saying you have to do

variables.less

@var1: value1;
@var2: value2;

module1.less

@import 'variables';
selectorA {
    propertyX: @var1
}

module2.less

@import 'variables';
selectorB {
    propertyY: @var2
}

you can approach it like this:

all.less (other common names for this are main.less and app.less)

@import 'variables';
@import 'module1';
@import 'module2';

variables.less

@var1: value1;
@var2: value2;

module1.less

selectorA {
    propertyX: @var1
}

module2.less

selectorB {
    propertyY: @var2
}