ant-design-vue/docs/vue/use-with-vue-cli.en-US.md

177 lines
4.6 KiB
Markdown
Raw Normal View History

2018-09-14 02:04:08 +00:00
# Use in vue-cli 3
2018-04-09 14:49:58 +00:00
2018-04-10 04:08:41 +00:00
[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.
2018-04-09 14:49:58 +00:00
## Install and Initialization
2018-04-10 04:08:41 +00:00
We need to install `vue-cli` first, you may need install [yarn](https://github.com/yarnpkg/yarn/) too.
2018-04-09 14:49:58 +00:00
```bash
2018-09-14 02:04:08 +00:00
$ npm install -g @vue/cli
# OR
$ yarn global add @vue/cli
2018-04-09 14:49:58 +00:00
```
Create a new project named `antd-demo`.
```bash
$ vue create antd-demo
2018-04-09 14:49:58 +00:00
```
2018-09-14 02:04:08 +00:00
And, setup your vue project configuration.
2019-09-28 12:45:07 +00:00
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.
2018-04-09 14:49:58 +00:00
Then we go inside `antd-demo` and start it.
```bash
$ cd antd-demo
2018-09-14 02:04:08 +00:00
$ npm run serve
2018-04-09 14:49:58 +00:00
```
2018-04-10 04:08:41 +00:00
Open the browser at http://localhost:8080/. It renders a header saying "Welcome to Your Vue.js App" on the page.
2018-04-09 14:49:58 +00:00
## Import antd
Below is the default directory structure.
```
├── README.md
├── babel.config
2018-04-09 14:49:58 +00:00
├── package.json
├── public
│   ├── favicon.ico
│   └── index.html
2018-04-09 14:49:58 +00:00
├── src
2018-04-10 04:08:41 +00:00
│   ├── assets
│ │ └── logo.png
│   ├── components
│ │ └── HelloWorld.vue
│   ├── App.vue
│   └── main.js
2018-04-09 14:49:58 +00:00
└── yarn.lock
```
Now we install `ant-design-vue` from yarn or npm.
2018-04-09 14:49:58 +00:00
```bash
$ yarn add ant-design-vue
2018-04-09 14:49:58 +00:00
```
2018-04-10 04:08:41 +00:00
Modify `src/main.js`, import Button component from `antd`.
2018-04-09 14:49:58 +00:00
```jsx
2019-09-28 12:45:07 +00:00
import Vue from 'vue';
import Button from 'ant-design-vue/lib/button';
import 'ant-design-vue/dist/antd.css';
import App from './App';
2018-04-09 14:49:58 +00:00
2018-12-20 01:34:11 +00:00
Vue.component(Button.name, Button);
2018-04-09 14:49:58 +00:00
2018-12-20 01:34:11 +00:00
Vue.config.productionTip = false;
2018-04-09 14:49:58 +00:00
2018-04-10 04:08:41 +00:00
/* eslint-disable no-new */
new Vue({
2019-09-28 12:45:07 +00:00
el: '#app',
2018-04-10 04:08:41 +00:00
components: { App },
2019-09-28 12:45:07 +00:00
template: '<App/>',
2018-12-20 01:34:11 +00:00
});
2018-04-10 04:08:41 +00:00
```
2018-12-20 01:34:11 +00:00
2018-04-10 04:08:41 +00:00
Modify `src/App.vue`
2018-04-09 14:49:58 +00:00
2018-04-10 04:08:41 +00:00
```jsx
<template>
<div id="app">
<img src="./assets/logo.png">
<a-button type="primary">Button></a-button>
<router-view/>
</div>
</template>
2018-04-09 14:49:58 +00:00
...
```
2018-04-10 04:08:41 +00:00
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).
2018-04-09 14:49:58 +00:00
## Advanced Guides
2019-09-28 12:45:07 +00:00
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.
2018-04-09 14:49:58 +00:00
2018-04-10 04:08:41 +00:00
Now we need to customize the default webpack config.
2018-04-09 14:49:58 +00:00
### Use babel-plugin-import
2019-02-16 08:51:21 +00:00
[babel-plugin-import](https://github.com/ant-design/babel-plugin-import) is a babel plugin for importing components on demand ([How does it work?](/docs/vue/getting-started/#Import-on-Demand)).
2018-04-09 14:49:58 +00:00
```bash
$ yarn add babel-plugin-import --dev
```
2018-12-20 01:34:11 +00:00
#### if you use vue-cli 2
2018-04-10 04:08:41 +00:00
Modify `.babelrc`.
2018-04-09 14:49:58 +00:00
```diff
2018-04-10 04:08:41 +00:00
{
"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": "ant-design-vue", "libraryDirectory": "es", "style": "css" }]
2018-04-10 04:08:41 +00:00
+ ]
2018-04-09 14:49:58 +00:00
}
```
2018-12-20 01:34:11 +00:00
#### if you use vue-cli 3
Modify `babel.config.js`
```diff
module.exports = {
presets: ["@vue/app"],
+ plugins: [
+ [
+ "import",
+ { libraryName: "ant-design-vue", libraryDirectory: "es", style: true }
+ ]
+ ]
};
```
Remove the `import 'ant-design-vue/dist/antd.css';` statement added before because `babel-plugin-import` will import styles and import components like below:
2018-04-09 14:49:58 +00:00
```diff
2018-04-10 04:08:41 +00:00
// src/main.js
import Vue from 'vue'
- import Button from 'ant-design-vue/lib/button';
+ import { Button } from 'ant-design-vue';
- import 'ant-design-vue/dist/antd.css'
2018-04-10 04:08:41 +00:00
import App from './App'
2018-04-09 14:49:58 +00:00
2018-04-10 04:08:41 +00:00
Vue.component(Button.name, Button)
2018-04-09 14:49:58 +00:00
2018-04-10 04:08:41 +00:00
Vue.config.productionTip = false
2018-04-09 14:49:58 +00:00
2018-04-10 04:08:41 +00:00
/* eslint-disable no-new */
new Vue({
el: '#app',
components: { App },
template: '<App/>'
})
```
2018-04-09 14:49:58 +00:00
2019-02-16 08:51:21 +00:00
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](/docs/vue/getting-started/#Import-on-Demand).
2018-04-09 14:49:58 +00:00
2018-04-10 04:08:41 +00:00
### Customize Theme
2018-04-09 14:49:58 +00:00
2019-02-16 08:51:21 +00:00
According to the [Customize Theme documentation](/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).