使用 uni-drawer 组件需手动安装注册,H5 需配置 transpileDependencies;Vue 3 中 ref 调用 open() 需加 .value;width 必须为带单位字符串;内容滚动须用带显式高度的 scroll-view;App Android 返回键需 onBackPress 手动 close。
直接用 uni-drawer 组件是最稳的路径,但它不是内置组件,不装不注册就写标签会白屏或报 unknown custom element 错误。
uni-drawer 必须手动安装和注册
很多人一写 <uni-drawer></uni-drawer> 就没反应,根本原因是没走安装+注册流程:
- 运行
pnpm add @dcloudio/uni-ui(或npm install @dcloudio/uni-ui) - 在
main.js中全局注册:import { uniDrawer } from '@dcloudio/uni-ui'; Vue.component('uni-drawer', uniDrawer); - H5 端还要检查
vue.config.js是否把@dcloudio/uni-ui加进了transpileDependencies,否则 ES6+ 语法可能不转译 - 别只复制
.vue文件进/components/——那是旧版插件市场用法,现在必须走@dcloudio/uni-ui包
Vue 3 里 ref 调用 open() 必须带 .value
Vue 2 写 this.$refs.drawer.open(),Vue 3 不行,ref() 返回的是响应式对象,得解包:
- 模板中:
<uni-drawer ref="drawerRef"></uni-drawer> - setup 里:
const drawerRef = ref(); drawerRef.value.open(); - 如果用了
script setup,记得defineExpose({ drawerRef })(虽然组件本身不依赖,但你封装成子组件时容易漏) - 别传
v-model想让它自动开——v-model只响应状态,不能触发打开动作
width 必须是字符串,且单位要明确
width 属性类型是 String,传数字或没单位会失效:
- 错:
:width="300"→ 宽度为 0 - 对:
:width="'300rpx'"或:width="'70%'" - H5 和小程序推荐统一用
rpx;nvue 页面只认px,需条件编译::width="uni.getSystemInfoSync().platform === 'ios' ? '320px' : '300px'" - 设
width="100%"时,父容器要加position: relative,否则抽屉可能溢出
内容滚动必须用 scroll-view + 固定高度
抽屉里内容超长却滚不动?不是组件问题,是滚动容器没达标:
- 必须用
<scroll-view></scroll-view>包裹内容,<view> + overflow-y: auto</view>在小程序和 nvue 下无效 -
scroll-view必须设显式高度,比如style="height: calc(100vh - 100rpx);",不能靠flex: 1或100vh(iOS 小程序里vh不可靠) - nvue 页面下,
scroll-view是唯一选择;H5 和小程序还建议加enable-back-to-top="false"防止下拉触发页面回顶 - 微信小程序中,给
scroll-view加enhanced="true"可提升滚动流畅度
最常被忽略的一点:App 端 Android 返回键不会自动关抽屉,必须在 onBackPress 里手动判断并调用 close(),否则点返回直接退出应用。











