官網:鏈接
安裝:推薦使用 npm
npm i element-ui -s
ElementUI 為新版的 vue-cli 準備了相應的 Element插件,可以快速搭建一個基于 Element 的項目。
引入 Element
可以引入整個 Element,或者根據需要引入部分組件。
- 完整引入
在 vue-blog main.js 中寫入
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
這里需要注意的是 樣式文件 需要單獨引入。
- 按需引入
借助 babel-plugin-component 可以只引入需要的組件,以達到減小項目體積的目的。
首先應該安裝 babel-plugin-component:
npm install babel-plugin-component -D
然后將 .babelrc 修改為:
{
"presets": [["es2015", { "modules": false }]],
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
}
接著把希望引入的部分組件寫入 main.js:
import { Button, Select } from 'element-ui'; // 比如:Button 和 Select
Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
/* 或寫為
* Vue.use(Button)
* Vue.use(Select)
*/
有一些常用的組件已經綁定到 Vue 的原型上,可以使用 this.$組件
來使用這些常用組件,或者寫 組件.something
。
詳細情況如下:
Vue.prototype.$loading = Loading.service;
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$notify = Notification;
Vue.prototype.$message = Message;
使用組件
在文檔中找到需要使用的組件的相應代碼,加入到 vue 文件中:
<el-button @click="onClick1">Button 組件測試</el-button>
可以使用 Element 中的默認樣式,也可以通過修改底層代碼或者CSS覆蓋樣式的方式來修改 Element 組件的樣式。
在 js 文件中綁定事件:
export default {
data() {
},
methods: {
onClick1() {
this.$message('這是我使用的第一個 Element 組件')
}
}
}
效果如下:
組件測試.PNG