vue3 vant移动端适配

本文介绍了如何将Vant中的px单位转换为rem单位,推荐使用postcss-pxtorem和lib-flexible两个工具。首先,通过yarn或npm安装amfe-flexible和postcss-pxtorem。然后,在main.js中引入amfe-flexible,并在postcss.config.js中配置postcss-pxtorem,根据文件是否包含'vant'来设定不同的根字体大小。这样可以确保在不同设备上保持样式的一致性。

Vant 中的样式默认使用 px 作为单位,如果需要使用 rem 单位,推荐使用以下两个工具:

postcss-pxtorem 是一款 postcss 插件,用于将 px 单位转化为 rem
lib-flexible 用于设置 rem 基准值

1.安装依赖

yarn add amfe-flexible
或者使用
npm i amfe-flexible

然后在 main.js 中加载执行该模块:

import 'amfe-flexible'

2.安装postcss-pxtorem依赖:

yarn add -D postcss-pxtorem
或者是
npm install postcss-pxtorem -D
这里解释一下:# -D 是 --save-dev 的简写 把依赖包的版本信息写进 package.json 文件里面

然后在项目根目录中创建 postcss.config.js 文件:

module.exports = {
    plugins: {
        // postcss-pxtorem 插件的版本需要 >= 5.0.0
        'postcss-pxtorem': {
            rootValue({ file }) { // 判断是否是vant的文件 如果是就使用 37.5为根节点字体大小
                // 否则使用75 因为vant使用的设计标准为375 但是市场现在的主流设置尺寸是750
                return file.indexOf('vant') !== -1 ? 37.5 : 75;
            },
            // 配置哪些文件中的尺寸需要转化为rem *表示所有的都要转化
            propList: ['*'],
        },
    },
};
这个文件会被自执行
<think> 我们正在构建一个使用Vue3Vant移动端项目。以下是详细的构建流程: 1. 创建Vue3项目 2. 安装Vant 3. 引入Vant组件(按需引入) 4. 移动端适配(例如使用viewport适配方案或rem适配方案) 5. 运行和构建 下面我将逐步说明。 </think> 以下是使用 Vue 3Vant 搭建移动端项目的完整构建流程,包含最佳实践和优化方案: ### 1. 环境准备 ```bash # 安装 Node.js (推荐 v16+) node -v # 安装 Vue CLI npm install -g @vue/cli ``` ### 2. 创建 Vue 3 项目 ```bash vue create vue3-vant-mobile ``` 选择配置: ``` ? Please pick a preset: Default ([Vue 3] babel, eslint) ❯ Default ([Vue 2] babel, eslint) Manually select features # 选择手动配置 ``` 勾选: ``` ◉ Babel ◉ TypeScript # 推荐 ◉ Vuex ◉ Router ◉ CSS Pre-processors ◉ Linter ``` ### 3. 安装 Vant ```bash cd vue3-vant-mobile npm install vant@next ``` ### 4. Vant 按需引入(关键步骤) ```bash npm install babel-plugin-import -D ``` 配置 `babel.config.js`: ```js module.exports = { plugins: [ [&#39;import&#39;, { libraryName: &#39;vant&#39;, libraryDirectory: &#39;es&#39;, style: true }, &#39;vant&#39;] ] } ``` ### 5. 移动端适配方案(推荐两种方案) **方案一:Viewport 适配(推荐)** ```bash npm install postcss-px-to-viewport -D ``` 创建 `postcss.config.js`: ```js module.exports = { plugins: { &#39;postcss-px-to-viewport&#39;: { viewportWidth: 375, // 设计稿宽度 selectorBlackList: [&#39;ignore&#39;] // 忽略的类名 } } } ``` **方案二:Rem 适配** ```bash npm install lib-flexible postcss-pxtorem -D ``` 在 `main.ts` 添加: ```ts import &#39;lib-flexible/flexible&#39; ``` 配置 `postcss.config.js`: ```js module.exports = { plugins: { &#39;postcss-pxtorem&#39;: { rootValue: 37.5, propList: [&#39;*&#39;] } } } ``` ### 6. 基础配置优化 **修改 `vite.config.ts`(或 `vue.config.js`):** ```ts export default defineConfig({ css: { preprocessorOptions: { less: { javascriptEnabled: true, modifyVars: { hack: `true; @import "~vant/es/style/var.less";` // 导入 Vant 变量 } } } } }) ``` ### 7. 创建示例组件 `src/components/HelloVant.vue`: ```vue <template> <van-button type="primary" @click="showDialog">显示弹窗</van-button> <van-dialog v-model:show="show" title="标题" message="欢迎使用Vant" /> </template> <script setup lang="ts"> import { ref } from &#39;vue&#39;; import { Button as VanButton, Dialog as VanDialog } from &#39;vant&#39;; const show = ref(false); const showDialog = () => show.value = true; </script> ``` ### 8. 全局样式调整 `src/styles/mobile.css`: ```css /* 禁用长按菜单 */ * { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; } /* 1px边框解决方案 */ .van-hairline--bottom::after { border-bottom-width: 0.5px !important; } ``` ### 9. 启动项目 ```bash npm run dev ``` 访问 `http://localhost:3000` 查看效果 ### 10. 构建生产包 ```bash npm run build ``` 生成的 `dist/` 目录包含优化后的静态资源 ### 最佳实践 1. **组件按需引入**:避免全量导入,减少打包体积 2. **主题定制**:在 `postcss.config.js` 修改 Vant 主题变量 ```js modifyVars: { &#39;@blue&#39;: &#39;#07c160&#39; // 示例:修改主题色 } ``` 3. **移动端调试**:安装 `vconsole` ```bash npm install vconsole ``` 在 `main.ts` 添加: ```ts import VConsole from &#39;vconsole&#39; new VConsole() ``` ### 常见问题解决 - **点击延迟**:安装 `fastclick` ```bash npm install fastclick ``` ```ts import { attach } from &#39;fastclick&#39; attach(document.body) ``` - **安全区域适配**:使用 Vant 的 `<van-safe-area />` 组件 - **图片懒加载**:使用 `<van-image lazy-load>` 组件 实际项目结构示例: ``` ├── public ├── src │ ├── views # 页面组件 │ ├── components # 通用组件 │ ├── store # Vuex 状态管理 │ ├── router # 路由配置 │ ├── styles # 全局样式 │ └── utils # 工具函数 ``` [^1]: Vant 官方文档推荐按需引入方案 [^2]: Vue CLI 官方构建配置指南 [^3]: 移动端适配最佳实践方案
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值