Element UI
是饿了么团队提供的一套基于Vue2.0
的组件库,可以快速的搭建网站,提高开发的效率
ElementUI PC
端
MintUI
移动端
一、全部引入
- 安装
element-ui
cnpm install element-ui -S
- 在
main.js
中引入并使用组件
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css' //该样式文件需要单独引入
Vue.use(ElementUI); //使用ElementUI
- 在
webpack.config.js
中添加loader
css
样式和字体图标都需要由相应的loader
来加载,所以需要配置style-loader
、css-loader
和file-loader
,内容:
{
test: /\.css$/, //加载样式
loader: 'style-loader!css-loader'
},
{
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/, //加载字体
loader: 'file-loader'
}
默认并没有安装style-loader
,所以需要单独安装该模块:
cnpm install style-loader -D
注:修改webpack.config.js
文件后需要重启服务器 npm run dev
- 使用组件
main.js
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css' //该样式文件需要单独引入
import App from './App.vue'
Vue.use(ElementUI); //使用ElementUI
new Vue({
el: '#app',
render: h => h(App)
})
DatePick.vue
<template>
<div id="datapicker">
<el-date-picker
v-model="value"
type="date"
placeholder="选择日期"
:picker-options="options"
size="small"
format="yyyy年MM月dd日">
</el-date-picker>
</div>
</template>
<script>
export default {
data(){
return {
value:'',
options:{
firstDayOfWeek:1,
disabledDate(time) {
return time.getTime() < Date.now() - 8.64e7;
}
}
}
}
}
</script>
二、按需引入
- 准备工作
vue init webpack-simple element-demo2//webpack-simple模板类型可按自己的项目选择;element-demo2项目名称,可自己取
cd element-demo2//切换到项目目录下
cnpm install
cnpm install element-ui --save
cnpm install style-loader --save-dev//在webpack.config.js中添加loader
npm run dev
cnpm install babel-plugin-component -D
- 配置
.babelrc
文件
"plugins": [["component", [
{
"libraryName": "element-ui",
"styleLibraryName": "theme-default"
}
]]]
- 只引入需要的组件
import {Button,Input,Radio,Select,Option,Rate,Table,TableColumn,Pagination,Carousel,CarouselItem} from 'element-ui'
Vue.use(Button);
Vue.use(Input);
Vue.use(Radio);
Vue.use(Select);
Vue.use(Option);
Vue.use(Rate);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(Pagination);
Vue.use(Carousel);
Vue.use(CarouselItem);
webpack.config.js
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loader: 'file-loader'
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
devServer: {
historyApiFallback: true,
noInfo: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
.babelrc
{
"presets": [
["latest", {
"es2015": { "modules": false }
}]
],
"plugins": [["component", [
{
"libraryName": "element-ui",
"styleLibraryName": "theme-default"
}
]]]
}