You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/docs/vue/use-with-vue-cli.en-US.md

158 lines
4.2 KiB

7 years ago
7 years ago
# Use in vue-cli
7 years ago
7 years ago
[vue-cli](https://github.com/vuejs/vue-cli) is one of the best Vue application development tools. We are going to use `antd` within it and modify the webpack config for some customized needs.
7 years ago
---
## Install and Initialization
7 years ago
We need to install `vue-cli` first, you may need install [yarn](https://github.com/yarnpkg/yarn/) too.
7 years ago
```bash
7 years ago
$ npm install -g vue-cli yarn
7 years ago
```
Create a new project named `antd-demo`.
```bash
7 years ago
$ vue init webpack antd-demo
7 years ago
```
The tool will create and initialize environment and dependencies automatically,
please try config your proxy setting or use another npm registry if any network errors happen during it.
Then we go inside `antd-demo` and start it.
```bash
$ cd antd-demo
7 years ago
$ npm run dev
7 years ago
```
7 years ago
Open the browser at http://localhost:8080/. It renders a header saying "Welcome to Your Vue.js App" on the page.
7 years ago
## Import antd
Below is the default directory structure.
```
├── README.md
├── package.json
7 years ago
├── index.html
7 years ago
├── src
7 years ago
│   ├── assets
│ │ └── logo.png
│   ├── components
│ │ └── HelloWorld.vue
│   ├── App.vue
│   └── main.js
7 years ago
└── yarn.lock
```
7 years ago
Now we install `vue-antd-ui` from yarn or npm.
7 years ago
```bash
7 years ago
$ yarn add vue-antd-ui
7 years ago
```
7 years ago
Modify `src/main.js`, import Button component from `antd`.
7 years ago
```jsx
7 years ago
import Vue from 'vue'
import Button from 'vue-antd-ui/lib/button'
import 'vue-antd-ui/dist/antd.css'
import App from './App'
7 years ago
7 years ago
Vue.component(Button.name, Button)
7 years ago
7 years ago
Vue.config.productionTip = false
7 years ago
7 years ago
/* eslint-disable no-new */
new Vue({
el: '#app',
components: { App },
template: '<App/>'
})
```
Modify `src/App.vue`
7 years ago
7 years ago
```jsx
<template>
<div id="app">
<img src="./assets/logo.png">
<a-button type="primary">Button></a-button>
<router-view/>
</div>
</template>
7 years ago
...
```
7 years ago
Ok, you should now see a blue primary button displayed on the page. Next you can choose any components of `antd` to develop your application. Visit other workflows of `vue-cli` at its [User Guide ](https://github.com/vuejs/vue-cli/blob/master/README.md).
7 years ago
## Advanced Guides
We are successfully running antd components now but in the real world, there are still lots of problems about antd-demo.
For instance, we actually import all styles of components in the project which may be a network performance issue.
7 years ago
Now we need to customize the default webpack config.
7 years ago
### Use babel-plugin-import
7 years ago
[babel-plugin-import](https://github.com/ant-design/babel-plugin-import) is a babel plugin for importing components on demand ([How does it work?](/ant-design/docs/vue/getting-started#Import-on-Demand)).
7 years ago
```bash
$ yarn add babel-plugin-import --dev
```
7 years ago
Modify `.babelrc`.
7 years ago
```diff
7 years ago
{
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
}
}],
"stage-2"
],
- "plugins": ["transform-vue-jsx", "transform-runtime"]
+ "plugins": [
+ "transform-vue-jsx",
+ "transform-runtime",
+ ["import", { "libraryName": "vue-antd-ui", "libraryDirectory": "es", "style": "css" }]
+ ]
7 years ago
}
```
7 years ago
Remove the `import 'vue-antd-ui/dist/antd.css';` statement added before because `babel-plugin-import` will import styles and import components like below:
7 years ago
```diff
7 years ago
// src/main.js
import Vue from 'vue'
- import Button from 'vue-antd-ui/lib/button';
+ import { Button } from 'vue-antd-ui';
- import 'vue-antd-ui/dist/antd.css'
import App from './App'
7 years ago
7 years ago
Vue.component(Button.name, Button)
7 years ago
7 years ago
Vue.config.productionTip = false
7 years ago
7 years ago
/* eslint-disable no-new */
new Vue({
el: '#app',
components: { App },
template: '<App/>'
})
```
7 years ago
7 years ago
Then reboot with `npm run dev` and visit the demo page, you should not find any [warning messages](https://zos.alipayobjects.com/rmsportal/vgcHJRVZFmPjAawwVoXK.png) in the console, which prove that the `import on demand` config is working now. You will find more info about it in [this guide](/ant-design/docs/vue/getting-started#Import-on-Demand).
7 years ago
7 years ago
### Customize Theme
7 years ago
7 years ago
According to the [Customize Theme documentation](/ant-design/docs/vue/customize-theme), to customize the theme, we need to modify `less` variables with tools such as [less-loader](https://github.com/webpack/less-loader).