安装依赖
在cmd命令中
npm i less --save-dev
随后安装less解析器
npm i less-loader@6 --save-dev
在main.js里全局配置
main.js import less from 'less'
Vue.use(less)
在独立的文件里啊引用:
<style lang="less">
</style>
less中变量的使用 定义方式:
@key:value
使用方式为:
@key
//字符串拼接变量使用方式 @img:'./img/'; background:url("@{img}1.png") url里面必须要使用引号(单双引号都可以)
多层嵌套+变量计算;
<div class="box1">
<div class="box2">
<div class="box3"></div>
</div>
</div>
<style lang="less">
@k:100px;
.box1{
width: @k;
height:@k;
background: red;
.box2{
width: @k/2;
height:@k/2;
background: green;
.box3{
width: @k/3;
height:@k/3;
background: blue;
}
}
}
</style>
混合 = 函数:
<div class="box1">我是box1</div>
<div class="box2">我是box2</div>
<style lang="less">
//定义一个函数;
.test(@color:red,@size:14px){
background: @color;
font-size:@size;
}
.box1{
// 不传参,使用默认的;
.test()
}
.box2{
// 给函数传参;
.test(@color:green,@size:30px)
}
</style>
可以对高度、宽度、角度进行计算;
//写减法的时候左右要加空格,否则会理解为杠-
<ul>
<li v-for="item in 4">{{item}}</li>
</ul>
<style lang="less" scoped>
@k:10px;
ul{
list-style: none;
li{
border:1px solid ;
margin:10px 0 ;
}
li:nth-child(1){
width: @k + @k;
height:@k;
}
li:nth-child(2){
width: @k -5px;
height:@k;
}
li:nth-child(3){
width: @k * @k;
height:@k;
}
li:nth-child(4){
width: @k / 2;
height:@k;
}
}
</style>
第一种方式 使用导入式 引入样式库
<style scoped lang="less">
@import url(./less/common.less);
</style>
第二种引入方式 在script中导入样式
import './less/common.less'
export default {
}
</script>