溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue中elementUI實(shí)現(xiàn)自定義主題的方法

發(fā)布時(shí)間:2020-10-26 09:27:29 來源:億速云 閱讀:354 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)Vue中elementUI實(shí)現(xiàn)自定義主題的方法的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。

                                                           使用vue開發(fā)項(xiàng)目,用到elementUI,根據(jù)官網(wǎng)的寫法,我們可以自定義主題來適應(yīng)我們的項(xiàng)目要求,下面來介紹一下兩種方法實(shí)現(xiàn)的具體步驟,,先說項(xiàng)目中沒有使用scss編寫,用主題工具的方法

第一種方法:使用命令行主題工具

使用vue-cli安裝完項(xiàng)目并引入element-ui(具體可參考第二種方法中的介紹)

一、安裝工具

1,安裝主題工具

npm i element-theme -g

2,安裝chalk主題,可以從 npm 安裝或者從 GitHub 拉取最新代碼

# 從 npm
npm i element-theme-chalk -D
# 從 GitHub
npm i https://github.com/ElementUI/theme-chalk -D

二、初始化變量文件

et -i [可以自定義變量文件,默認(rèn)為element-variables.scss]
> ? Generator variables file

這時(shí)根目錄下會(huì)產(chǎn)生element-variables.scss(或自定義的文件),大致如下:

$--color-primary: #409EFF !default;
$--color-primary-light-1: mix($--color-white, $--color-primary, 10%) !default; /* 53a8ff */
$--color-primary-light-2: mix($--color-white, $--color-primary, 20%) !default; /* 66b1ff */
$--color-primary-light-3: mix($--color-white, $--color-primary, 30%) !default; /* 79bbff */
$--color-primary-light-4: mix($--color-white, $--color-primary, 40%) !default; /* 8cc5ff */
$--color-primary-light-5: mix($--color-white, $--color-primary, 50%) !default; /* a0cfff */
$--color-primary-light-6: mix($--color-white, $--color-primary, 60%) !default; /* b3d8ff */
$--color-primary-light-7: mix($--color-white, $--color-primary, 70%) !default; /* c6e2ff */
$--color-primary-light-8: mix($--color-white, $--color-primary, 80%) !default; /* d9ecff */
$--color-primary-light-9: mix($--color-white, $--color-primary, 90%) !default; /* ecf5ff */
$--color-success: #67c23a !default;
$--color-warning: #eb9e05 !default;
$--color-danger: #fa5555 !default;
$--color-info: #878d99 !default;
...

三、修改變量

直接編輯 element-variables.scss 文件,例如修改主題色為自己所需要的顏色(如: 紫色(purple))

$--color-primary: purple;

四、編譯主題

修改完變量后,要編譯主題(如果編譯后,再次修改了變量,需要重新編譯)

et
> ? build theme font
> ? build element theme

五、引入自定義主題

最后一步,將編譯好的主題文件引入項(xiàng)目(編譯的文件默認(rèn)在根目錄下的theme文件下,也可以通過 -o 參數(shù)指定打包目錄),在入口文件main.js中引入

import '../theme/index.css'
import ElementUI from 'element-ui'
import Vue from 'vue'
Vue.use(ElementUI)

在項(xiàng)目中寫些樣式,看下主題色是否改變:(主題色變?yōu)樽仙?/p>

<p>
  <el-button>默認(rèn)按鈕</el-button>
  <el-button type="primary">主要按鈕</el-button>
  <el-button type="success">成功按鈕</el-button>
  <el-button type="info">信息按鈕</el-button>
  <el-button type="warning">警告按鈕</el-button>
  <el-button type="danger">危險(xiǎn)按鈕</el-button>
 </p>

第二種方法: 直接修改element樣式變量

在項(xiàng)目中直接修改element的樣式變量,(前提是你的文檔也是使用scss編寫)

一、首先用vue-cli安裝一個(gè)新項(xiàng)目:

1,安裝vue:

npm i -g vue

2,在項(xiàng)目目錄下安裝vue-cli:

npm i -g vue-cli

3,基于webpack建立新項(xiàng)目( vue-project)

vue init webpack vue-project

4,依次輸入以下命令行,運(yùn)行vue-project

cd vue-project
npm i
npm run dev

二、安裝elementUI以及sass-loader,node-sass(項(xiàng)目中使用scss編寫需要依賴的插件)

1,安裝element-ui

npm i element-ui -S

2,安裝sass-loader,node-sass

npm i sass-loader node-sass -D

在這里說一下,不需要配置webpack.base.conf.js文件,vue-loader會(huì)根據(jù)不同類型文件來配置相應(yīng)loader來打包我們的樣式文件(感興趣的可看下vue-loader的核心代碼)

三、改變element樣式變量

1.在src下建立element-variables.scss文件(名字可以自定義),寫入如下代碼:

/* 改變主題色變量 */
$--color-primary: teal;
/* 改變 icon 字體路徑變量,必需 */
$--font-path: '../node_modules/element-ui/lib/theme-chalk/fonts';
@import "../node_modules/element-ui/packages/theme-chalk/src/index";

2.在入口文件main.js中引入上面的文件即可

import Vue from 'vue'
import Element from 'element-ui'
import './element-variables.scss'
Vue.use(Element)

看下效果吧,在文件里引入些樣式看看,如button

<p>
  <el-button>默認(rèn)按鈕</el-button>
  <el-button type="primary">主要按鈕</el-button>
  <el-button type="success">成功按鈕</el-button>
  <el-button type="info">信息按鈕</el-button>
  <el-button type="warning">警告按鈕</el-button>
  <el-button type="danger">危險(xiǎn)按鈕</el-button>
 </p>

默認(rèn)的顏色已經(jīng)變?yōu)槲覀冏远x的了,有其他的改變?cè)趀lement-variable.scss文件中改變變量即可

感謝各位的閱讀!關(guān)于Vue中elementUI實(shí)現(xiàn)自定義主題的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI