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
2016-11-13 03:58:45 +00:00
We provide a general [project template ](https://github.com/ElementUI/element-starter ) for you. For those who are familiar with [cooking ](https://github.com/ElementUI/element-cooking-starter ) or [Laravel ](https://github.com/ElementUI/element-in-laravel-starter ), we also provide corresponding templates, and you can download and use them as well.
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
If you prefer not to use them, please read the following.
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
### Config files
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
Create a new project, and its structure should be
```text
|- src/ --------------------- source code
|- App.vue
|- main.js -------------- entry
|- .babelrc ----------------- babel config
|- index.html --------------- HTML template
|- package.json ------------- npm config
|- README.md ---------------- readme
2016-11-13 14:00:55 +00:00
|- webpack.config.js ------ webpack config
2016-11-13 03:58:45 +00:00
```
Typical configurations for these config files are:
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
**.babelrc**
```json
{
"presets": [
["es2015", { "modules": false }]
]
}
2016-11-10 13:46:55 +00:00
```
2016-11-13 03:58:45 +00:00
< br >
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
**package.json**
```json
{
"name": "my-project",
"description": "A Vue.js and Element project",
"private": true,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --inline --hot --port 8086",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"element-ui": "^1.0.0",
"vue": "^2.0.5"
},
"devDependencies": {
"babel-core": "^6.0.0",
"babel-loader": "^6.0.0",
"babel-preset-es2015": "^6.13.2",
"cross-env": "^1.0.6",
"css-loader": "^0.23.1",
"file-loader": "^0.8.5",
"style-loader": "^0.13.1",
"vue-loader": "^9.5.1",
"webpack": "2.1.0-beta.22",
"webpack-dev-server": "^2.1.0-beta.0"
}
}
```
< br >
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
**webpack.config.js**
2016-11-10 13:46:55 +00:00
```javascript
2016-11-13 03:58:45 +00:00
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'
},
resolveLoader: {
root: path.join(__dirname, 'node_modules')
},
module: {
loaders: [
{
test: /\.vue$/,
loader: 'vue'
},
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style!css'
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file'
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file',
query: {
name: '[name].[ext]?[hash]'
}
}
]
},
devServer: {
historyApiFallback: true,
noInfo: true
},
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({
compress: {
warnings: false
}
})
])
}
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'
import 'element-ui/lib/theme-default/index.css'
import App from './App.vue'
2016-11-10 13:46:55 +00:00
2016-11-13 03:58:45 +00:00
Vue.use(MintUI)
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",
"styleLibraryName": "theme-default"
}
]]]
}
```
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
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.