RootPortal 根节点传送 v0.2.2+
19:21使整个子树从页面中脱离出来,类似于在 CSS 中使用 fixed position 的效果。主要用于制作弹窗、弹出层等。
平台差异说明
| App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 头条小程序 | QQ小程序 |
|---|---|---|---|---|---|---|
| √ | √ | √ | √ | × | × | × |
- H5 端:使用
teleport特性,详见teleport文档 - 微信小程序和支付宝小程序:使用
root-portal组件,详见微信小程序root-portal和支付宝小程序root-portal - App 端:使用 renderjs 实现
- 其他平台:不支持此功能
提示
根节点传送组件仅支持微信小程序、支付宝小程序、APP和H5平台,组件会自动根据平台选择合适的实现方式:
这类场景最常见的例子就是全屏的模态框。理想情况下,我们希望触发模态框的按钮和模态框本身的代码是在同一个单文件组件中,因为它们都与组件的开关状态有关。但这意味着该模态框将与按钮一起渲染在应用 DOM 结构里很深的地方。这会导致该模态框的 CSS 布局代码很难写。
基本用法
使用 u-root-portal 将内容渲染到根节点,避免被父组件的样式影响。
html
<u-button type="primary" @click="show = true">显示弹窗</u-button>
<u-root-portal v-if="show">
<view class="modal">
<view class="modal-content">
<text>这是一个全局弹窗</text>
<u-button @click="show = false">关闭</u-button>
</view>
</view>
</u-root-portal>scss
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
.modal-content {
background-color: #fff;
padding: 20px;
border-radius: 8px;
text-align: center;
}API
Slots
| 名称 | 说明 |
|---|---|
| default | 默认插槽,用于渲染传送内容 |