美文网首页
1.ant组件导入方式

1.ant组件导入方式

作者: 静昕妈妈芦培培 | 来源:发表于2021-04-07 13:44 被阅读0次

1.完整导入

import antd from 'ant-design-vue'; 的写法引入了 antd 下所有的模块,这会影响应用的网络性能。

import Vue from 'vue'
import App from './App.vue'

//完整导入:一次性导入所有的组件
import antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'; //需要注意的是,样式文件需要单独引入。
Vue.use(antd)

Vue.config.productionTip = false

new Vue({
  render: h => h(App)
}).$mount('#app')

使用:

<template>
  <div class="hello">
    <a-button type="primary" @click="btnClick">antd按钮 </a-button>
    <a-input placeholder="请输入"></a-input>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  methods: {
    btnClick() {
      this.$message.info('这是一条消息')
    }
  }
}
</script>

<style scoped>

</style>

效果:点击按钮,弹窗了消息


image.png

2.局部导入

2.1 在main.js按需导入组件button message及其样式

import Vue from 'vue'
import App from './App.vue'

// 按需加载组件
import Button from 'ant-design-vue/lib/button'
import message from 'ant-design-vue/lib/message'
import 'ant-design-vue/lib/button/style' // 或者 ant-design-vue/lib/button/style/css 加载 css 文件
import 'ant-design-vue/lib/message/style'

Vue.use(Button)
Vue.prototype.$message = message

Vue.config.productionTip = false

new Vue({
  render: h => h(App)
}).$mount('#app')

2.2 使用:

<template>
  <div class="hello">
    <a-button type="primary" @click="btnClick">antd按钮 </a-button>
    <a-input placeholder="请输入"></a-input>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  methods: {
    btnClick() {
      this.$message.info('这是一条消息')
    }
  }
}
</script>

<style scoped>

</style>

2.3 效果:点击按钮,弹窗了消息


image.png

如果你使用如下方式,并不是按需加载,依然是整体导入了所有的组件

import { Button } from 'ant-design-vue'; 的写法引入了 antd 下所有的模块,这会影响应用的网络性能。
2.1 main.js

import Vue from 'vue'
import App from './App.vue'

//局部导入:只导入在项目中使用的组件
import { Button, Input, message } from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'; //导入ant样式文件
Vue.use(Button)
Vue.use(Input)
Vue.prototype.$message = message

Vue.config.productionTip = false

new Vue({
  render: h => h(App)
}).$mount('#app')

2.2 使用:

<template>
  <div class="hello">
    <a-button type="primary" @click="btnClick">antd按钮 </a-button>
    <a-input placeholder="请输入"></a-input>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  methods: {
    btnClick() {
      this.$message.info('这是一条消息')
    }
  }
}
</script>

<style scoped>

</style>

2.3 效果:点击按钮,弹窗了消息


image.png

3.按需加载

3.1安装babel-plugin-import插件

npm install babel-plugin-import --save-dev

3.2babel.config.js配置babel-plugin-import插件

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: [
    ['import', {
      libraryName: 'ant-design-vue',
      libraryDirectory: 'es',
      style: true //默认导入的样式文件为less文件
    }]
  ]
}

注意:因为style:true, 默认导入的式less样式文件,需要安装less-loader来解析less文件

npm install less-loader@6.0.0 --save-dev

此处我指定了less-loader版本,因为不指定版本会默认安装最新版本,安装会失败报错,而安装less-loader的4,3版本,ant定制主体会失败
如果要导入css文件,使用如下配置:

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: [
    ['import', {
      libraryName: 'ant-design-vue',
      libraryDirectory: 'es',
      style: (name) => `${name}/style/css` //修改导入的样式文件为css文件
    }]
  ]
}

3.3按需加载ant组件

import Vue from 'vue'
import App from './App.vue'

//按需引入:babel-plugin-import会帮你转换为import Button from 'ant-design-vue/lib/button'
import {Button} from 'ant-design-vue'
Vue.use(Button)

Vue.config.productionTip = false

new Vue({
  render: h => h(App)
}).$mount('#app')

使用:

<template>
  <div class="hello">
    <a-button type="primary" @click="btnClick">antd按钮 </a-button>
    <a-input placeholder="请输入"></a-input>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  methods: {
    btnClick() {
      this.$message.info('这是一条消息')
    }
  }
}
</script>

<style scoped>

</style>

相关文章

  • 1.ant组件导入方式

    1.完整导入 import antd from 'ant-design-vue'; 的写法引入了 antd 下所有...

  • Vue动态加载组件的四种方式

    动态加载组件的四种方式: 1、使用import导入组件,可以获取到组件 2、使用import导入组件,直接将组件赋...

  • vue-cli3使用svg的最佳实践

    目录 iconfont的三种使用方式及其优缺点 封装svg组件 svg雪碧图 自动导入svg图标 封装svg组件 ...

  • 使用springMVC实现文件上传

    简单思路 导入文件上传依赖组件 注册文件上传组件 在springmvc配置文件中加入 修改前端表单提交方式以及请求...

  • Vue.js 路由

    声明式导入 以 ES6 方式导入 vue 原生路由组件 插件安装 因为 vue-router 被定义为插件,所以在...

  • ElementUI二次封装Table组件

    table-cmp组件 JS 使用方法 1、导入组件 2、模板中加入导入组件 3、data数据

  • React-Native基础知识

    1. 组件的导入导出 1.1 组件的导出 组件的导出使用关键字export default 1.2 组件的导入 组...

  • React: 富文本

    项目使用的是React,所以用的react-quill组件 1、安装 2、导入组件 3、导入样式 4、使用组件 页...

  • vue-router

    使用平级组件 1.定义两个vue组件2.在父组件中import导入这两个组件3.导入路由组件vue-router4...

  • excel/csv 导出与导入(包含日期处理)

    1.基本介绍 excel的导出导入,使用了组件jxls;csv的导出导入,使用了组件opencsv。组件是挺好用,...

网友评论

      本文标题:1.ant组件导入方式

      本文链接:https://www.haomeiwen.com/subject/agdekltx.html