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

179 lines
4.6 KiB

# Use in vue-cli 3
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
$ npm install -g @vue/cli
# OR
$ yarn global add @vue/cli
7 years ago
```
Create a new project named `antd-demo`.
```bash
$ vue create antd-demo
7 years ago
```
And, setup your vue project configuration.
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
$ npm run serve
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
├── babel.config
7 years ago
├── package.json
├── public
│   ├── favicon.ico
│   └── index.html
7 years ago
├── src
7 years ago
│   ├── assets
│ │ └── logo.png
│   ├── components
│ │ └── HelloWorld.vue
│   ├── App.vue
│   └── main.js
7 years ago
└── yarn.lock
```
Now we install `ant-design-vue` from yarn or npm.
7 years ago
```bash
$ yarn add ant-design-vue
7 years ago
```
7 years ago
Modify `src/main.js`, import Button component from `antd`.
7 years ago
```jsx
import Vue from "vue";
import Button from "ant-design-vue/lib/button";
import "ant-design-vue/dist/antd.css";
import App from "./App";
7 years ago
Vue.component(Button.name, Button);
7 years ago
Vue.config.productionTip = false;
7 years ago
7 years ago
/* eslint-disable no-new */
new Vue({
el: "#app",
7 years ago
components: { App },
template: "<App/>"
});
7 years ago
```
7 years ago
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
[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)).
7 years ago
```bash
$ yarn add babel-plugin-import --dev
```
#### if you use vue-cli 2
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": "ant-design-vue", "libraryDirectory": "es", "style": "css" }]
7 years ago
+ ]
7 years ago
}
```
#### 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:
7 years ago
```diff
7 years ago
// 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'
7 years ago
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
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).
7 years ago
7 years ago
### Customize Theme
7 years ago
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).