Skip to content

Navbar 自定义导航栏组件

此组件一般用于在特殊情况下,需要自定义导航栏的时候用到,一般建议使用uni-app带的导航栏。

温馨提示

本项目参考了 uView-Plus 开源项目的组件开发方式,基于 Vue 3 和 TypeScript 实现了自定义组件。目前该组件仍处于测试阶段。
感谢 uView-Plus 开源项目及其团队成员的贡献,他们的组件开发思路为本项目提供了宝贵地参考。如果需要了解更多组件开发细节,可以参考uView-Plus的 navbar组件 的代码实现。

平台差异说明

APP(vue)H5微信小程序

基本使用示例

html
<!-- 全局使用 -->
<hy-navbar title="个人中心"></hy-navbar>
<!-- 单个组件引入 -->
<HyNavbar title="个人中心"></HyNavbar>
ts
import { HyNavbar } from "hy-app"

防止塌陷

  • 通过配置placeholder在固定在顶部时生成一个等高元素,以防止塌陷
html
<template>
    <hy-navbar title="个人中心" placeholder></hy-navbar>
</template>

背景色

html
<template>
    <hy-navbar title="个人中心" bgColor="#001f3f"></hy-navbar>
</template>

固定在顶部

  • 通过配置field导航栏在固定在顶部时
html
<template>
    <hy-navbar title="个人中心" fixed></hy-navbar>
</template>

基本使用示例

  • 通过配置title定义导航栏标题
  • 通过配置leftIcon定义导航栏左边图标
  • 通过配置leftText定义导航栏左边文字
  • 通过配置rightText定义导航栏右边文字
  • 通过配置rightIcon定义导航栏右边图标
html
<template>
    <hy-navbar 
        title="文档"
        :leftIcon="IconConfig.LEFT"
        leftText="返回"
        rightText="地址"
        :rightIcon="IconConfig.MAP"
    ></hy-navbar>
</template>

<script setup>
    import { IconConfig } from "hy-app";
</script>

自定义左边插槽

html
<template>
    <hy-navbar title="自定义插槽" :fixed="false" bg-color="#F8F8F8">
        <template #left>
            <view class="u-nav-slot">
                <hy-icon :name="IconConfig.LEFT" size="16"></hy-icon>
                <hy-line
                        direction="column"
                        :hairline="false"
                        length="16"
                        margin="0 8px"
                ></hy-line>
                <hy-icon name="home" size="15"></hy-icon>
            </view>
        </template>
    </hy-navbar>
</template>

<style lang="scss">
    .u-nav-slot {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        border-radius: 100px;
        border: 1rpx solid gainsboro;
        padding: 3px 7px;
        opacity: 0.8;
    }
</style>

API

参数说明类型默认值
safeAreaInsetTop是否开启顶部安全区适配booleantrue
placeholder固定在顶部时,是否生成一个等高元素,以防止塌陷booleanfalse
fixed导航栏是否固定在顶部booleantrue
border导航栏底部是否显示下边框booleanfalse
leftIcon左边返回图标的名称stringIconConfig.LEFT
leftText左边的提示文字string-
rightText右边的提示文字string-
rightIcon右边返回图标的名称string-
title导航栏标题,如设置为空字符,将会隐藏标题占位区域string-
bgColor导航栏背景设置string#ffffff
titleWidth导航栏标题的最大宽度,内容超出会以省略号隐藏,单位rpxstring | number400rpx
height导航栏高度(不包括状态栏高度在内,内部自动加上),单位pxstring | number44px
leftIconSize左侧返回图标的大小string | number20
leftIconColor左侧返回图标的颜色string-
autoBack点击左侧区域(返回图标),是否自动返回上一页booleanfalse
titleStyle标题的样式,对象或字符串形式CSSProperties-
customStyle定义需要用到的外部样式CSSProperties-

Events

事件名说明回调参数
leftClick点击左侧区域-
rightClick点击右侧区域-

Slots

插槽名说明接收值
left自定义左侧部分内容-
right自定义右侧部分内容-
center自定义中部内容-
00:46