首頁  >  文章  >  web前端  >  在Vue中使用elementUI實作自訂主題方法

在Vue中使用elementUI實作自訂主題方法

亚连
亚连原創
2018-06-05 09:27:162025瀏覽

下面我就為大家分享一篇Vue的elementUI實現自訂主題方法,具有很好的參考價值,希望對大家有幫助。

使用vue開發項目,用到elementUI,根據官網的寫法,我們可以自訂主題來適應我們的專案要求,以下來介紹一下兩種方法實現的具體步驟,(可以參考官方文件自定義主題官方文件),先說專案中沒有使用scss編寫,用主題工具的方法(使用的較多)

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

使用vue-cli安裝完專案並引入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 [可以自定义变量文件,默认为element-variables.scss]
> ✔ Generator variables file

這時根目錄下會產生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

五、引入自訂主題

最後一步,將編譯好的主題檔案引入專案(編譯的檔案預設在根目錄下的theme檔案下,也可以透過-o 參數指定打包目錄),在入口檔案main.js中引入

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

在專案中寫些樣式,看下主題色是否改變:(主題色變為紫色)

<p>
  <el-button>默认按钮</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">危险按钮</el-button>
 </p>

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

在專案中直接修改element的樣式變數, (前提是你的文件也是用scss寫)

一、先用vue-cli安裝一個新項目:

##1,安裝vue:

npm i -g vue

2,在專案目錄下安裝vue-cli:

npm i -g vue-cli

3,基於webpack建立新專案( vue-project)

vue init webpack vue-project

4,依序輸入以下命令列,執行vue-project

cd vue-project
npm i
npm run dev

二、安裝elementUI以及sass-loader,node-sass(專案中使用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會根據不同類型檔案來設定對應loader來打包我們的樣式檔案(有興趣的可看下vue-loader的核心程式碼)

三、改變element樣式變數

1.在src下建立element-variables.scss檔(名字可以自訂),寫入如下程式碼:

/* 改变主题色变量 */
$--color-primary: teal;
/* 改变 icon 字体路径变量,必需 */
$--font-path: &#39;../node_modules/element-ui/lib/theme-chalk/fonts&#39;;
@import "../node_modules/element-ui/packages/theme-chalk/src/index";

2.在入口檔案main.js中引入上面的檔案即可

import Vue from &#39;vue&#39;
import Element from &#39;element-ui&#39;
import &#39;./element-variables.scss&#39;
Vue.use(Element)

看下效果吧,在檔案裡引入些樣式看看,如button

<p>
  <el-button>默认按钮</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">危险按钮</el-button>
 </p>

預設的顏色已經變成我們自訂的了,有其他的改變在element-variable.scss檔中改變變數即可

上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

透過在js動態中建立標籤,並設定屬性方法(詳細教學)

在jQuery中實現標籤子元素的加入與賦值方法

在jQuery如何改變P標籤文字值

以上是在Vue中使用elementUI實作自訂主題方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn