≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

Vue的elementUI实现自定义主题方法

人气:460 时间:2019-04-09

这篇文章主要为大家详细介绍了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)

在项目中写些样式,看下主题色是否改变:(主题色变为紫色)

代码如下:


<div>
  <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>
 </div>

 

第二种方法: 直接修改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: '../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

代码如下:


<div>
  <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>
 </div>

默认的颜色已经变为我们自定义的了,有其他的改变在element-variable.scss文件中改变变量即可

以上这篇Vue的elementUI实现自定义主题方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持四海网。

本文来自:http://www.q1010.com/184/4563-0.html

注:关于Vue的elementUI实现自定义主题方法的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • vue编译打包本地查看index文件的方法
  • Vue之Vue.set动态新增对象属性方法
  • vue 路由页面之间实现用手指进行滑动的方法
  • vue-cli中的babel配置文件.babelrc实例分析
  • vue.js2.0点击获取自己的属性和jquery方法
  • vue 使用Jade模板写html,stylus写css的方法
  • elementui的默认样式修改方法
  • vue cli webpack中使用sass的方法
  • 使用watch监听路由变化和watch监听对象的实例
  • Vue 创建组件的两种方法小结(必看)
上一篇:element-ui 表格实现单元格可编辑的示例
下一篇:使用watch监听路由变化和watch监听对象的实例
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。