2016-11-13 03:58:45 +00:00
## Quick start
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
This part walks you through the process of using Element in a webpack project.
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
### Use Starter Kit
2016-11-10 13:46:55 +00:00
2017-10-27 03:30:04 +00:00
We provide a general [project template ](https://github.com/ElementUI/element-starter ) for you. For Laravel users, we also have a [template ](https://github.com/ElementUI/element-in-laravel-starter ). You can download and use them directly.
If you prefer not to use them, please read the following.
2016-11-10 13:46:55 +00:00
2017-10-26 04:50:28 +00:00
### Use vue-cli
2016-11-10 13:46:55 +00:00
2017-10-27 03:30:04 +00:00
We can also start a project using [vue-cli ](https://github.com/vuejs/vue-cli ):
2016-11-13 03:58:45 +00:00
2017-10-26 04:50:28 +00:00
```shell
> npm i -g vue-cli
> mkdir my-project && cd my-project
> vue init webpack
> npm i && npm i element-ui
2016-11-10 13:46:55 +00:00
```
2016-11-13 03:58:45 +00:00
### Import Element
You can import Element entirely, or just import what you need. Let's start with fully import.
#### Fully import
In main.js:
2016-11-10 13:46:55 +00:00
```javascript
2016-11-13 03:58:45 +00:00
import Vue from 'vue'
import ElementUI from 'element-ui'
2017-09-28 10:01:29 +00:00
import 'element-ui/lib/theme-chalk/index.css'
2016-11-13 03:58:45 +00:00
import App from './App.vue'
2016-11-10 13:46:55 +00:00
2016-11-16 12:06:02 +00:00
Vue.use(ElementUI)
2016-11-13 03:58:45 +00:00
new Vue({
el: '#app',
render: h => h(App)
})
2016-11-10 13:46:55 +00:00
```
2016-11-13 03:58:45 +00:00
The above imports Element entirely. Note that CSS file needs to be imported separately.
#### 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.
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
First, install babel-plugin-component:
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
```bash
npm install babel-plugin-component -D
```
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
Then edit .babelrc:
2016-11-10 13:46:55 +00:00
```json
{
2016-11-13 03:58:45 +00:00
"presets": [
["es2015", { "modules": false }]
],
"plugins": [["component", [
2016-11-10 13:46:55 +00:00
{
"libraryName": "element-ui",
2017-09-28 10:01:29 +00:00
"styleLibraryName": "theme-chalk"
2016-11-10 13:46:55 +00:00
}
]]]
}
```
2016-11-13 03:58:45 +00:00
Next, if you need Button and Select, edit main.js:
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
```javascript
import Vue from 'vue'
import { Button, Select } from 'element-ui'
import App from './App.vue'
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
Vue.component(Button.name, Button)
Vue.component(Select.name, Select)
/* or
* Vue.use(Button)
* Vue.use(Select)
*/
new Vue({
el: '#app',
render: h => h(App)
})
```
2016-11-10 13:46:55 +00:00
2017-09-29 07:37:50 +00:00
Full example (Component list reference [components.json ](https://github.com/ElemeFE/element/blob/carbon/components.json ))
2017-03-28 07:18:51 +00:00
```javascript
import Vue from 'vue'
import {
Pagination,
Dialog,
Autocomplete,
Dropdown,
DropdownMenu,
DropdownItem,
Menu,
Submenu,
MenuItem,
MenuItemGroup,
Input,
InputNumber,
Radio,
RadioGroup,
RadioButton,
Checkbox,
2017-09-29 07:37:50 +00:00
CheckboxButton,
2017-03-28 07:18:51 +00:00
CheckboxGroup,
Switch,
Select,
Option,
OptionGroup,
Button,
ButtonGroup,
Table,
TableColumn,
DatePicker,
TimeSelect,
TimePicker,
Popover,
Tooltip,
Breadcrumb,
BreadcrumbItem,
Form,
FormItem,
Tabs,
TabPane,
Tag,
Tree,
Alert,
Slider,
Icon,
Row,
Col,
Upload,
Progress,
Badge,
Card,
Rate,
Steps,
Step,
Carousel,
CarouselItem,
Collapse,
CollapseItem,
Cascader,
2017-04-15 18:26:47 +00:00
ColorPicker,
2017-09-29 07:37:50 +00:00
Transfer,
Container,
Header,
Aside,
Main,
Footer,
2017-04-15 18:26:47 +00:00
Loading,
MessageBox,
2017-07-07 10:29:59 +00:00
Message,
Notification
2017-03-28 07:18:51 +00:00
} from 'element-ui'
Vue.use(Pagination)
Vue.use(Dialog)
Vue.use(Autocomplete)
Vue.use(Dropdown)
Vue.use(DropdownMenu)
Vue.use(DropdownItem)
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItem)
Vue.use(MenuItemGroup)
Vue.use(Input)
Vue.use(InputNumber)
Vue.use(Radio)
Vue.use(RadioGroup)
Vue.use(RadioButton)
Vue.use(Checkbox)
Vue.use(CheckboxGroup)
Vue.use(Switch)
Vue.use(Select)
Vue.use(Option)
Vue.use(OptionGroup)
Vue.use(Button)
Vue.use(ButtonGroup)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(DatePicker)
Vue.use(TimeSelect)
Vue.use(TimePicker)
Vue.use(Popover)
Vue.use(Tooltip)
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Tabs)
Vue.use(TabPane)
Vue.use(Tag)
Vue.use(Tree)
Vue.use(Alert)
Vue.use(Slider)
Vue.use(Icon)
Vue.use(Row)
Vue.use(Col)
Vue.use(Upload)
Vue.use(Progress)
Vue.use(Badge)
Vue.use(Card)
Vue.use(Rate)
Vue.use(Steps)
Vue.use(Step)
Vue.use(Carousel)
Vue.use(CarouselItem)
Vue.use(Collapse)
Vue.use(CollapseItem)
Vue.use(Cascader)
Vue.use(ColorPicker)
2017-09-29 07:37:50 +00:00
Vue.use(Container)
Vue.use(Header)
Vue.use(Aside)
Vue.use(Main)
Vue.use(Footer)
2017-03-28 07:18:51 +00:00
Vue.use(Loading.directive)
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
```
2017-10-16 10:05:40 +00:00
### Global config
When importing Element, you can define a global config object. For now this object has only one property: `size` , which sets the default size for all components:
Fully import Element:
```JS
import Vue from 'vue'
import Element from 'element-ui'
Vue.use(Element, { size: 'small' })
```
Partial import Element:
```JS
import Vue from 'vue'
import { Button } from 'element-ui'
Vue.prototype.$ELEMENT = { size: 'small' }
Vue.use(Button)
```
With the above config, the default size of all components that have size attribute will be 'small'.
2016-11-13 03:58:45 +00:00
### Start coding
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
Now you have implemented Vue and Element to your project, and it's time to write your code. Start development mode:
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
```bash
# visit localhost:8086
npm run dev
2016-11-10 13:46:55 +00:00
```
2016-11-13 03:58:45 +00:00
Build:
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
```bash
npm run build
```
Please refer to each component's documentation to learn how to use them.