Vant 移动端UI

Vue项目中安装Vant

Vue 3 项目,安装最新版

Vant npm i vant

组件按需引入配置

vant按需引入- - -安装"unplugin-vue-components" 插件

Vite 中使用 Vant 的话,可以使用 "unplugin-vue-components" 插件,这个插件可以按需引入用到的Vant组件,使用Vant 组件时直接在 template模板 里使用就可以,不用手动 在 ts 脚本里 import导入

1.安装插件

通过 npm 安装

npm i @vant/auto-import-resolver unplugin-vue-components -D

通过 yarn 安装

yarn add @vant/auto-import-resolver unplugin-vue-components -D

通过 pnpm 安装

pnpm add @vant/auto-import-resolver unplugin-vue-components -D

通过 bun 安装

bun add @vant/auto-import-resolver unplugin-vue-components -D

2.配置插件

1、如果是基于 vite 的项目,在 vite.config.js 文件中配置插件:

javascript 复制代码
import vue from '@vitejs/plugin-vue';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';

export default {
  plugins: [
    vue(),
    Components({
      resolvers: [VantResolver()],
    }),
  ],
};

2、如果是基于 vue-cli 的项目,在 vue.config.js 文件中配置插件:

javascript 复制代码
const { VantResolver } = require('@vant/auto-import-resolver');
const ComponentsPlugin = require('unplugin-vue-components/webpack');

module.exports = {
  configureWebpack: {
    plugins: [
      ComponentsPlugin({
        resolvers: [VantResolver()],
      }),
    ],
  },
};

3、如果是基于 webpack 的项目,在 webpack.config.js 文件中配置插件:

javascript 复制代码
const { VantResolver } = require('@vant/auto-import-resolver');
const ComponentsPlugin = require('unplugin-vue-components/webpack');

module.exports = {
  plugins: [
    ComponentsPlugin({
      resolvers: [VantResolver()],
    }),
  ],
};

组件中使用

1、示例:

html 复制代码
<template>
   <van-button type="primary" >按钮 </van-button>
</template>

2、函数式组件使用注意事项

Vant 中 Toast,Dialog,Notify 和 ImagePreview 组件是函数式组件,需要 import 引入后,再使用,且要引入组件样式,例如:

html 复制代码
<script setup lang="ts">
import { showToast } from 'vant';

showToast('请填写正确的手机号');

</script>

mian.ts 中引入组件样式:

javascript 复制代码
import 'vant/es/toast/style';

其他函数式组件引用示例:

javascript 复制代码
// Dialog
import { showDialog } from 'vant';
import 'vant/es/dialog/style';

// Notify
import { showNotify } from 'vant';
import 'vant/es/notify/style';

// ImagePreview
import { showImagePreview } from 'vant';
import 'vant/es/image-preview/style';

3、Vant 组件样式相关问题解决方法

1.修改vant 组件样式方法:

引用组件后,在浏览器中打开调试窗口,找到对应要修改样式的组件元素,查看组件元素的类名,重写该类名样式

有时不生效可能是权重不够,叠加类名或者样式添加 " !important "

示例:

css 复制代码
.van-cell {
  font-size: 28px!important;
  background: transparent!important;
}

2.vant表单组件清除组件很难点,经常点上去没有清除效果

可能是被遮盖了,设置表单的右边margin 大于 清除图标的宽度:

示例:

css 复制代码
.van-field__control {
  margin-right: 15px!important;
}

3.vant 表单清除按钮 PC端失效

1、安装模块

npm i @vant/touch-emulator -S

2、在 " main.ts " 文件中引入模块后自动生效

import '@vant/touch-emulator'

安装引入后如果没有立即生效,可以重启一下项目,再查看效果

查阅Vant 文档了解到 vant是针对移动端的,PC端下有些组件可能会失效,可以安装 "vant/touch-emulator" 模块解决。

Vant 有 Vue 2 版本、Vue 3 版本、微信小程序版本、 React 版本、支付宝小程序版本。

了解更多vant先关内容可以查阅Vant官方文档~

相关推荐
小白小白从不日白6 分钟前
react 组件通讯
前端·react.js
罗_三金17 分钟前
前端框架对比和选择?
javascript·前端框架·vue·react·angular
Redstone Monstrosity24 分钟前
字节二面
前端·面试
东方翱翔31 分钟前
CSS的三种基本选择器
前端·css
Fan_web1 小时前
JavaScript高级——闭包应用-自定义js模块
开发语言·前端·javascript·css·html
yanglamei19621 小时前
基于GIKT深度知识追踪模型的习题推荐系统源代码+数据库+使用说明,后端采用flask,前端采用vue
前端·数据库·flask
千穹凌帝1 小时前
SpinalHDL之结构(二)
开发语言·前端·fpga开发
冯宝宝^1 小时前
基于mongodb+flask(Python)+vue的实验室器材管理系统
vue.js·python·flask
dot.Net安全矩阵1 小时前
.NET内网实战:通过命令行解密Web.config
前端·学习·安全·web安全·矩阵·.net
叫我:松哥1 小时前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap