vue2中l(wèi)ess的安裝以及使用教程
1.安裝
1.1 在vue cli2 使用vue init webpack xxx 創(chuàng)建的項目
webpack安裝要考慮less-loader的兼容問題,
npm install less-loader@7.0.0
如果7.0.0不行,就一步一步下探,6.0.0 5.0.0
安裝less-loader成功后,
npm install --save less
安裝less
在webpack.base.config.js的rules里添加
{
test: /\.less$/,
loader: "style-loader!css-loader!less-loader"
}
在.vue文件的style區(qū),加上lang='less'
1.2 在vue cli3中(vue create xxx)
npm install less-loader -- save-dev
npm install less --save-dev
在.vue文件的style區(qū),加上lang='less'
2.使用
2.1定義變量
@width: 500px; @height: @width+10px; @red: #f00; @transparent: 0.5; @radius: 30px;
注意變量也可以引用變量
2.2 在屬性中使用變量
#app {
width: @width;
height: @height;
background-color: @red;
}
2.3混合(Mixin)
.border() {
border-radius: @radius;
border-color: @red;
}
.colors() {
primary: #3385ff;
secondary: green;
}
#app {
width: @width;
height: @height;
background-color: @red;
.border();
}編譯出來就是
#app {
width: 500px;
height: 510px;
background-color: #f00;
border-radius: 30px;
border-color: #f00;
}
2.4在引用變量時,做一些運算
.inner-app {
width: @width / 2;
height: @height / 2;
background-color: #00f;
}
編譯出來是
.inner-app {
width: 500px / 2;
height: 510px / 2;
background-color: #00f;
}
2.5可以以屬性名作為變量
.app2 {
@width: 200px;
width: @width;
height: $width;
background-color: .colors[secondary]; // .colors在上邊Mixin里定義的
}
編譯出來是
.app2 {
width: 200px;
height: 200px;
background-color: green;
}
2.6引用父元素以及祖先元素(&)
#app {
width: @width;
height: @height;
background-color: @red;
&:hover {
opacity: @transparent;
&::after {
content: "anc";
}
&::before {
content: "xyt";
}
}
}
編譯出來是
#app:hover {
opacity: 0.5;
}
#app:hover::after {
content: "anc";
}
#app:hover::before {
content: "xyt";
}
注意 &引用的是所有父元素和祖先元素
總結
到此這篇關于vue2中l(wèi)ess的安裝以及使用教程的文章就介紹到這了,更多相關vue2 less安裝使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺析vue-router jquery和params傳參(接收參數(shù))$router $route的區(qū)別
今天做項目時踩到了vue-router傳參的坑(jquery和params),所以決定總結一下二者的區(qū)別。感興趣的朋友跟隨腳本之家小編一起看看吧2018-08-08
VUE使用echarts?5.0以上版本渲染器未導入錯誤問題
這篇文章主要介紹了VUE使用echarts?5.0以上版本渲染器未導入錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
解決vue3中使用echart報錯:Cannot read properties of&n
在Vue項目中使用Echarts進行數(shù)據可視化是非常常見的需求,然而有時候在引入Echarts的過程中可能會遇到報錯,本文主要介紹了解決vue3中使用echart報錯:Cannot read properties of undefined (reading ‘type‘),感興趣的可以了解一下2024-01-01

