2020-08-13 15:18:26 +08:00
## Quick start
2020-10-16 11:14:34 +08:00
This part walks you through the process of using Element Plus in a webpack project.
2020-08-13 15:18:26 +08:00
2020-10-26 17:16:22 +08:00
### Use vue-cli@4.5
2020-08-13 15:18:26 +08:00
2020-10-26 17:16:22 +08:00
We provide an [Element Plus plugin ](https://github.com/element-plus/vue-cli-plugin-element-plus ) for vue-cli@4.5, which you can use to quickly build an Element-based project.
2020-08-13 15:18:26 +08:00
### Use Starter Kit
2020-10-27 18:26:07 +08:00
We provide a general [project template ](https://github.com/element-plus/element-plus-starter ) for you, and also a Vite [template ](https://github.com/element-plus/element-plus-vite-starter ). For Laravel users, we have a [template ](https://github.com/element-plus/element-plus-in-laravel-starter ) here. You can download and use them directly.
2020-08-13 15:18:26 +08:00
If you prefer not to use them, please read the following.
2020-10-16 11:14:34 +08:00
### Import Element Plus
2020-08-13 15:18:26 +08:00
2020-10-16 11:19:36 +08:00
You can import ElementPlus entirely, or just import what you need. Let's start with fully import.
2020-08-13 15:18:26 +08:00
#### Fully import
In main.js:
```javascript
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
2020-10-16 11:14:34 +08:00
import ElementPlus from 'element-plus';
import 'element-plus/lib/theme-chalk/index.css';
2020-08-13 15:18:26 +08:00
import App from './App.vue';
2020-10-16 11:14:34 +08:00
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
2020-08-13 15:18:26 +08:00
```
2020-10-16 11:14:34 +08:00
The above imports Element Plus entirely. Note that CSS file needs to be imported separately.
2020-08-13 15:18:26 +08:00
#### On demand
With the help of [babel-plugin-component ](https://github.com/QingWei-Li/babel-plugin-component ), we can import components we actually need, making the project smaller than otherwise.
First, install babel-plugin-component:
```bash
npm install babel-plugin-component -D
```
Then edit .babelrc:
```json
{
"plugins": [
[
"component",
{
2020-10-16 11:14:34 +08:00
"libraryName": "element-plus",
2020-08-13 15:18:26 +08:00
"styleLibraryName": "theme-chalk"
}
]
]
}
```
Next, if you need Button and Select, edit main.js:
```javascript
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
import { ElButton, ElSelect } from 'element-plus';
2020-08-13 15:18:26 +08:00
import App from './App.vue';
2020-11-23 16:28:06 +08:00
const app = createApp(App)
app.component(ElButton.name, ElButton);
app.component(ElSelect.name, ElSelect);
2020-08-13 15:18:26 +08:00
/* or
2020-11-23 16:28:06 +08:00
* app.use(ElButton)
* app.use(ElSelect)
2020-08-13 15:18:26 +08:00
*/
2020-11-23 16:28:06 +08:00
app.mount('#app')
2020-08-13 15:18:26 +08:00
```
2020-11-23 16:28:06 +08:00
Full example (Component list [reference ](https://github.com/element-plus/element-plus/tree/dev/packages ))
2020-08-13 15:18:26 +08:00
```javascript
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
import App from './App.vue';
2020-08-13 15:18:26 +08:00
import {
2020-11-23 16:28:06 +08:00
ElAlert,
ElAside,
ElAutocomplete,
ElAvatar,
ElBacktop,
ElBadge,
ElBreadcrumb,
ElBreadcrumbItem,
ElButton,
ElButtonGroup,
ElCalendar,
ElCard,
ElCarousel,
ElCarouselItem,
ElCascader,
ElCascaderPanel,
ElCheckbox,
ElCheckboxButton,
ElCheckboxGroup,
ElCol,
ElCollapse,
ElCollapseItem,
ElCollapseTransition,
ElColorPicker,
ElContainer,
ElDatePicker,
ElDialog,
ElDivider,
ElDrawer,
ElDropdown,
ElDropdownItem,
ElDropdownMenu,
ElFooter,
ElForm,
ElFormItem,
ElHeader,
ElIcon,
ElImage,
ElInput,
ElInputNumber,
ElLink,
ElMain,
ElMenu,
ElMenuItem,
ElMenuItemGroup,
ElOption,
ElOptionGroup,
ElPageHeader,
ElPagination,
ElPopconfirm,
ElPopover,
ElPopper,
ElProgress,
ElRadio,
ElRadioButton,
ElRadioGroup,
ElRate,
ElRow,
2020-12-04 17:07:26 +08:00
ElScrollbar,
2020-11-23 16:28:06 +08:00
ElSelect,
ElSlider,
ElStep,
ElSteps,
ElSubmenu,
ElSwitch,
ElTabPane,
ElTable,
ElTableColumn,
2020-12-01 19:14:11 +08:00
ElTabs,
ElTag,
ElTimePicker,
ElTimeSelect,
2020-11-23 16:28:06 +08:00
ElTimeline,
ElTimelineItem,
ElTooltip,
ElTransfer,
ElTree,
ElUpload,
ElInfiniteScroll,
ElLoading,
ElMessage,
ElMessageBox,
ElNotification,
2020-10-16 11:14:34 +08:00
} from 'element-plus';
2020-08-13 15:18:26 +08:00
2020-11-23 16:28:06 +08:00
const components = [
ElAlert,
ElAside,
ElAutocomplete,
ElAvatar,
ElBacktop,
ElBadge,
ElBreadcrumb,
ElBreadcrumbItem,
ElButton,
ElButtonGroup,
ElCalendar,
ElCard,
ElCarousel,
ElCarouselItem,
ElCascader,
ElCascaderPanel,
ElCheckbox,
ElCheckboxButton,
ElCheckboxGroup,
ElCol,
ElCollapse,
ElCollapseItem,
ElCollapseTransition,
ElColorPicker,
ElContainer,
ElDatePicker,
ElDialog,
ElDivider,
ElDrawer,
ElDropdown,
ElDropdownItem,
ElDropdownMenu,
ElFooter,
ElForm,
ElFormItem,
ElHeader,
ElIcon,
ElImage,
ElInput,
ElInputNumber,
ElLink,
ElMain,
ElMenu,
ElMenuItem,
ElMenuItemGroup,
ElOption,
ElOptionGroup,
ElPageHeader,
ElPagination,
ElPopconfirm,
ElPopover,
ElPopper,
ElProgress,
ElRadio,
ElRadioButton,
ElRadioGroup,
ElRate,
ElRow,
2020-12-04 17:07:26 +08:00
ElScrollbar,
2020-11-23 16:28:06 +08:00
ElSelect,
ElSlider,
ElStep,
ElSteps,
ElSubmenu,
ElSwitch,
ElTabPane,
ElTable,
ElTableColumn,
ElTabs,
ElTag,
ElTimePicker,
ElTimeSelect,
ElTimeline,
ElTimelineItem,
ElTooltip,
ElTransfer,
ElTree,
ElUpload,
]
const plugins = [
ElInfiniteScroll,
ElLoading,
ElMessage,
ElMessageBox,
ElNotification,
]
const app = createApp(App)
components.forEach(component => {
app.component(component.name, component)
})
plugins.forEach(plugin => {
app.use(plugin)
})
2020-08-13 15:18:26 +08:00
```
### Global config
When importing Element, you can define a global config object. For now this object has two properties: `size` and `zIndex` . The property `size` sets the default size for all components and the property `zIndex` sets the initial z-index (default: 2000) for modal boxes:
Fully import Element:
```js
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
2020-10-16 11:19:36 +08:00
import ElementPlus from 'element-plus';
2020-11-23 16:28:06 +08:00
import App from './App.vue';
const app = createApp(App)
app.use(ElementPlus, { size: 'small', zIndex: 3000 });
2020-08-13 15:18:26 +08:00
```
Partial import Element:
```js
2020-11-23 16:28:06 +08:00
import { createApp } from 'vue'
import { ElButton } from 'element-plus';
import App from './App.vue';
2020-08-13 15:18:26 +08:00
2020-11-23 16:28:06 +08:00
const app = createApp(App)
app.config.globalProperties.$ELEMENT = option
app.use(ElButton);
2020-08-13 15:18:26 +08:00
```
With the above config, the default size of all components that have size attribute will be 'small', and the initial z-index of modal boxes is 3000.
### Start coding
2020-10-16 11:14:34 +08:00
Now you have implemented Vue and Element Plus to your project, and it's time to write your code. Please refer to each component's documentation to learn how to use them.
2020-08-13 15:18:26 +08:00
### Use Nuxt.js
We can also start a project using [Nuxt.js ](https://nuxtjs.org/ ):
< div class = "glitch-embed-wrap" style = "height: 420px; width: 100%;" >
< iframe src = "https://glitch.com/embed/#!/embed/nuxt-with-element?path=nuxt.config.js&previewSize=0&attributionHidden=true" alt = "nuxt-with-element on glitch" style = "height: 100%; width: 100%; border: 0;" > < / iframe >
< / div >