element/examples/docs/en-US/quickstart.md

214 lines
4.5 KiB
Markdown
Raw Normal View History

2016-11-13 03:58:45 +00:00
## Quick start
2016-11-13 03:58:45 +00:00
This part walks you through the process of using Element in a webpack project.
2016-11-13 03:58:45 +00:00
### Use Starter Kit
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-13 03:58:45 +00:00
If you prefer not to use them, please read the following.
2016-11-13 03:58:45 +00:00
### Config files
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-16 06:09:25 +00:00
|- webpack.config.js -------- webpack config
2016-11-13 03:58:45 +00:00
```
Typical configurations for these config files are:
2016-11-13 03:58:45 +00:00
**.babelrc**
```json
{
"presets": [
["es2015", { "modules": false }]
]
}
```
2016-11-13 03:58:45 +00:00
<br>
2016-11-13 03:58:45 +00:00
**package.json**
```json
{
"name": "element-starter",
2016-11-13 03:58:45 +00:00
"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.1.6"
2016-11-13 03:58:45 +00:00
},
"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.8.0",
"webpack": "beta",
"webpack-dev-server": "beta"
2016-11-13 03:58:45 +00:00
}
}
```
<br>
2016-11-13 03:58:45 +00:00
**webpack.config.js**
```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'
},
module: {
loaders: [
{
test: /\.vue$/,
loader: 'vue-loader'
2016-11-13 03:58:45 +00:00
},
{
test: /\.js$/,
loader: 'babel-loader',
2016-11-13 03:58:45 +00:00
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
2016-11-13 03:58:45 +00:00
},
{
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loader: 'file-loader'
2016-11-13 03:58:45 +00:00
},
{
test: /\.(png|jpe?g|gif|svg)(\?\S*)?$/,
loader: 'file-loader',
2016-11-13 03:58:45 +00:00
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-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:
```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-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-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-13 03:58:45 +00:00
First, install babel-plugin-component:
2016-11-13 03:58:45 +00:00
```bash
npm install babel-plugin-component -D
```
2016-11-13 03:58:45 +00:00
Then edit .babelrc:
```json
{
2016-11-13 03:58:45 +00:00
"presets": [
["es2015", { "modules": false }]
],
"plugins": [["component", [
{
"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-13 03:58:45 +00:00
```javascript
import Vue from 'vue'
import { Button, Select } from 'element-ui'
import App from './App.vue'
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-13 03:58:45 +00:00
### Start coding
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-13 03:58:45 +00:00
```bash
# visit localhost:8086
npm run dev
```
2016-11-13 03:58:45 +00:00
Build:
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.