一、在vue中如何引入ant design
(1)完整引入
main.js
中全局引入並注冊
import Antd from 'ant-design-vue' import 'ant-design-vue/dist/antd.css' Vue.use(Antd)
- 在頁面中不再需要引入注冊組件,可以直接使用所有的組件
<template> <div> <a-button type="primary">hello world</a-button> </div> </template> <script> export default {} </script>
(2)導入部分組件
- 在
main.js
中導入並注冊需要在項目中使用的組件
import { Button } from "ant-design-vue"; import 'ant-design-vue/lib/button/style/css' Vue.component(Button.name, Button)
- 在項目中可以直接使用這個已經注冊的組件
<template> <div> <a-button type="primary">hello world</a-button> </div> </template> <script> export default {} </script>
(3)按需加載
ant-design-vue
使用babel-plugin-import
進行按需加載
- 安裝
babel-plugin-import
插件
npm i babel-plugin-import --save-dev
- 修改
.babelrc
文件,在plugins
節點下,添加下面這個配置項:
"plugins": ["transform-vue-jsx", "transform-runtime", [ "import", { "libraryName": "ant-design-vue", "libraryDirectory": "lib", "style": "css" } ] ]
- 在需要使用相關組件的頁面引入並注冊即可按需加載
<template> <div> <a-button type="primary">hello world</a-button> </div> </template> <script> import { Button } from 'ant-design-vue'; export default { components:{ AButton:Button }, } </script>