" just add less code."/> " just add less code.">
Home >Web Front-end >Vue.js >How to use less with vue.js?
The operating environment of this tutorial: windows7 system, vue2.9 version, this method is suitable for all brands of computers.
vue uses less
Dependency download
1. First use npm to download dependencies;
npm install --save less less-loader
2. After the installation is completed, check whether the installation is successful;
lessc -v
3. If the installation is successful, the version after successful installation will be displayed;
Reference method
1. In main.js
import less from 'less' Vue.use(less)
2. Then create a .vue file and we start playing;
Note : The independent vue file needs to introduce less
<style lang="less"></style>
to start using it
1. The use of variables in less;
In less, we are allowed to use Define the variable in the form of: @k:v; Usage: @k;
<p class="box"></p> <style lang="less"> @color:red; @k:100px; .box{ width:@k; height:@k; background: @color; } </style>
At this time, a square with a width of 100px, a height of 100px and a red background will be displayed on the page;
2. How to use string splicing variables;
<p class="box1"></p> <style lang="less" scoped> @img:'./img/'; @k:100px; .box1{ width:@k; height:@k; background:url("@{img}1.png") } </style>
Note: The path needs to be wrapped with "", @{img} usually introduces variables to take effect;
3. Multi-layer nested variable calculation;
<p class="box1"> <p class="box2"> <p class="box3"></p> </p> </p> <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>
As you can see, less can be used in nesting, so we can see it at once Understand the CSS structure; in addition to nested use, have you found that its calculation is really powerful?
4. Mix = function
<p class="box1">我是box1</p> <p class="box2">我是box2</p> <style lang="less"> //定义一个函数; .test(@color:red,@size:14px){ background: @color; font-size:@size; } .box1{ // 不传参,使用默认的; .test() } .box2{ // 给函数传参; .test(@color:green,@size:30px) } </style>
5. Matching pattern
<p class="box"></p> //定义的css <style lang="less"> .sjx(@_,@color,@size){ width: 0; height:0; border:@size solid @color; border-color:transparent; } //左边三角形 .sjx(l,@color,@size){ border-left-color:@color; } //上边三角形 .sjx(t,@color,@size){ border-top-color:@color; } //右边三角形 .sjx(r,@color,@size){ border-right-color:@color; } //左边三角形 .sjx(b,@color,@size){ border-bottom-color:@color; } //这里匹配调用 .box{ .sjx(r,red,20px) } </style>
box matching It is t (top), which is the upper one.
The box matches b (buttom), which is the lower one;
Summary The matching pattern is like the switch statement in js. Whatever is entered will be displayed; but it doesn’t matter what you feel;
7. Color function
<p>默认红色</p> <p>默认绿色</p> <ul> <li <li v-for="i in 6">测试</li> </ul> <span>混合</span> <style lang="less" scoped> *{ padding: 0; margin: 0; } @color:red; @color1:green; p:nth-child(1){ background: @color; }; p:nth-child(2){ background: @color1; }; ul{ list-style: none; li:nth-child(1){ background:lighten(@color,50%); } li:nth-child(2){ background:darken(@color,50%); } li:nth-child(3){ background:saturate(@color,50%); } li:nth-child(4){ background:desaturate(@color,50%); } li:nth-child(5){ background:spin(@color,50%); } li:nth-child(6){ background:spin(@color,50%); } } span{ background: mix(@color,@color1); } </style>
8. Operator
can calculate height, width, and angle;
<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>
For more programming-related knowledge, please visit: Programming course ! !
The above is the detailed content of How to use less with vue.js?. For more information, please follow other related articles on the PHP Chinese website!