• 【快应用】实现自定义导航栏组件


     快应用实现自定义导航栏组件。

    1. 简介

    导航栏组件,主要用于头部导航。

    导航栏(Nav_bar)组件结构大致分为两部分,一部分是图标,另一部分是文本,子组件实现,父组件引用。

    效果图如下:

    cke_710.png

    基本布局代码如下:

    1. <template>
    2. <div class="container">
    3. <text>本导航栏为自定义组件,并非原生导航栏。除非原生导航栏无法满足需求,否则不推荐使用自定义导航栏组件。text>
    4. <text class="section">基本用法text>
    5. <div class="example-body">
    6. <nav_bar height="50px" backgroundcolor="#ffffff" left-icon="{{leftIcon}}" title="标题"
    7. @clickleft="back" title-style="font-size: 40px; color:red">nav_bar>
    8. div>
    9. div>
    10. <template>

    2.开发指引

    2.1自定义子组件

    1.定义布局样式。

    导航栏组件布局包括三个部分:左侧图标内容部分、标题内容部分、右侧图标内容部分。

    左侧图标内容部分、右侧图标内容部分通过image+text+slot组件实现

    标题内容部分由text+slot组件实现。

    代码如下:

    1. <template>
    2. <div>
    3. <div class="navbar_content" style="height:{{height}};background-color:{{backgroundcolor}}">
    4. <div class="navbar_btns_left" onclick="clickLeft">
    5. <div if="leftIcon.length">
    6. <image style="height: 50px" src="{{leftIcon}}">image>
    7. div>
    8. <div if="leftText.length">
    9. <text style="{{leftTextStyle}}">{{ leftText }}text>
    10. div>
    11. <slot name="left">slot>
    12. div>
    13. <div class="navbar_container" onclick="clickTitle">
    14. <text class="text_context" style="{{titleStyle}}"
    15. if="title.length">{{ title }}text>
    16. <slot name="mid">slot>
    17. div>
    18. <div class="navbar_btns_right" onclick="clickRight">
    19. <div if="rightIcon.length">
    20. <image style="height: 50px" src="{{rightIcon}}">image>
    21. div>
    22. <div class="*" if="rightText.length && !rightIcon.length">
    23. <text style="{{rightTextStyle}}">{{ rightText }}text>
    24. div>
    25. <slot name="right">slot>
    26. div>
    27. div>
    28. div>
    29. template>

    2.2子组件设计

    支持的属性如下:

    属性名

    类型

    默认值

    说明

    title

    String

    null

    标题文字

    height 

    String

    null

    导航栏高度

    backgroundcolor

    String

    null

    导航栏背景色

    leftText

    String

    null

    左侧按钮文本

    rightText

    String

    null

    右侧按钮文本

    leftIcon

    String

    null

    左侧按钮图标

    rightIcon

    String

    null

    右侧按钮图标

    支持的事件:

    事件名称

    说明

    返回值

    clickLeft

    点击当前点击状态

    clickRight 

    点击当前点击状态

    clickTitle

    点击当前点击状态

    2.3父子组件通信

    2.3.1父组件给子组件传递数据

    子组件通过在props定义参数,接收来自父组件的传值数据,如height、title等。如下图所示:

      cke_16452.png 

    cke_21738.png

    cke_27159.png

    2.3.2子组件通过this.$emit方法触发父组件的自定义事件

    cke_33780.png

    3.总结

    实现导航栏组件,您可以从中学会如下知识点:

    l  熟悉快应用子组件的设计和属性定义;

    l  熟悉父子组件通信;

    l  熟悉slot组件的运用;

      

    想欲了解更多详情,请参见:

    华为快应用官网:

    文档中心

    最后附上完整的实现代码:

    导航栏组件nav_bar.ux

    1. <template>
    2. <div>
    3. <div class="navbar_content" style="height:{{height}};background-color:{{backgroundcolor}}">
    4. <div class="navbar_btns_left" onclick="clickLeft">
    5. <div if="leftIcon.length">
    6. <image style="height: 50px" src="{{leftIcon}}">image>
    7. div>
    8. <div if="leftText.length">
    9. <text style="{{leftTextStyle}}">{{ leftText }}text>
    10. div>
    11. <slot name="left">slot>
    12. div>
    13. <div class="navbar_container" onclick="clickTitle">
    14. <text class="text_context" style="{{titleStyle}}"
    15. if="title.length">{{ title }}text>
    16. <slot name="mid">slot>
    17. div>
    18. <div class="navbar_btns_right" onclick="clickRight">
    19. <div if="rightIcon.length">
    20. <image style="height: 50px" src="{{rightIcon}}">image>
    21. div>
    22. <div class="*" if="rightText.length && !rightIcon.length">
    23. <text style="{{rightTextStyle}}">{{ rightText }}text>
    24. div>
    25. <slot name="right">slot>
    26. div>
    27. div>
    28. div>
    29. template>
    30. <script>
    31. /**
    32. * NavBar 自定义导航栏
    33. * @description 导航栏组件,主要用于头部导航
    34. * @tutorial https://ext.dcloud.net.cn/plugin?id=52
    35. * @property {String} title 标题文字
    36. * @property {String} height 导航栏高度
    37. * @property {String} backgroundcolor 导航栏背景色
    38. * @property {String} leftText 左侧按钮文本
    39. * @property {String} rightText 右侧按钮文本
    40. * @property {String} leftIcon 左侧按钮图标
    41. * @property {String} rightIcon 右侧按钮图标
    42. * @property {String} leftTextStyle 左侧按钮文本样式
    43. * @property {String} titleStyle 中间标题文本样式
    44. * @property {String} rightTextStyle 右侧按钮文本样式
    45. * @event {Function} clickLeft 左侧按钮点击时触发
    46. * @event {Function} clickRight 右侧按钮点击时触发
    47. * @event {Function} clickTitle 中间标题点击时触发
    48. */
    49. module.exports = {
    50. props: {
    51. height: {
    52. type: String,
    53. default: ""
    54. },
    55. backgroundcolor: {
    56. type: String,
    57. default: ""
    58. },
    59. title: {
    60. type: String,
    61. default: ""
    62. },
    63. leftText: {
    64. type: String,
    65. default: ""
    66. },
    67. rightText: {
    68. type: String,
    69. default: ""
    70. },
    71. leftIcon: {
    72. type: String,
    73. default: ""
    74. },
    75. rightIcon: {
    76. type: String,
    77. default: ""
    78. },
    79. leftTextStyle: {
    80. type: String,
    81. default: ''
    82. },
    83. titleStyle: {
    84. type: String,
    85. default: ''
    86. },
    87. rightTextStyle: {
    88. type: String,
    89. default: ''
    90. },
    91. },
    92. onInit() {
    93. this.$page.setTitleBar({ text: '自定义导航栏' })
    94. },
    95. clickLeft() {
    96. this.$emit("clickleft");
    97. },
    98. clickRight() {
    99. this.$emit("clickright");
    100. },
    101. clickTitle() {
    102. this.$emit("clicktitle");
    103. }
    104. }
    105. script>
    106. <style>
    107. .navbar_content {
    108. display: flex;
    109. align-items: center;
    110. flex-direction: row;
    111. }
    112. .navbar_btns_left {
    113. width: 150px;
    114. }
    115. .navbar_container {
    116. width: 500px;
    117. }
    118. .text_context {
    119. width: 480px;
    120. text-align: center;
    121. }
    122. .navbar_btns_right {
    123. width: 150px;
    124. justify-content: flex-end;
    125. }
    126. style>

    主页面hello.ux

    1. <import name="nav_bar" src="./Nav_bar/nav_bar.ux">import>
    2. <template>
    3. <div class="container">
    4. <text>本导航栏为自定义组件,并非原生导航栏。除非原生导航栏无法满足需求,否则不推荐使用自定义导航栏组件。text>
    5. <text class="section">基本用法text>
    6. <div class="example-body">
    7. <nav_bar height="50px" backgroundcolor="#ffffff" left-icon="{{leftIcon}}" title="标题" @clickleft="back" title-style="font-size: 40px; color:red">nav_bar>
    8. div>
    9. <text class="section">左右显示文字text>
    10. <div class="example-body">
    11. <nav_bar left-icon="{{leftIcon}}" left-text="返回" title="标题" left-text-style="font-size: 30px; color:red;" right-text="菜单" @clickleft="back" @clickTitle="showTitle">nav_bar>
    12. div>
    13. <text class="section">插入slottext>
    14. <div class="example-body">
    15. <nav_bar right-icon="{{rightIcon}}" @clickleft="showCity" @clickright="scan">
    16. <div slot="left">
    17. <div>
    18. <text>北京text>
    19. <image src="../Common/arrowdown.png">image>
    20. div>
    21. div>
    22. <div slot="mid">
    23. <div class="input-view">
    24. <image style="height: 40px; margin-top: 15px" src="../Common/search.png">image>
    25. <input enterkeytype="search" placeholder="输入搜索关键词" @enterkeyclick="confirm" />
    26. div>
    27. div>
    28. nav_bar>
    29. div>
    30. div>
    31. template>
    32. <script>
    33. import router from '@system.router'
    34. import prompt from '@system.prompt'
    35. export default {
    36. data() {
    37. return {
    38. city: "BeiJing",
    39. leftIcon: "../Common/leftIcon.png",
    40. rightIcon: "../Common/rightIcon.png"
    41. }
    42. },
    43. back() {
    44. router.back()
    45. },
    46. scan() {
    47. prompt.showToast({
    48. message: '扫码',
    49. duration: "100000",
    50. gravity: 'center'
    51. })
    52. },
    53. showCity() {
    54. prompt.showToast({
    55. message: '选择城市',
    56. duration: "100000",
    57. gravity: 'center'
    58. })
    59. },
    60. showTitle() {
    61. prompt.showToast({
    62. message: '标题',
    63. duration: "100000",
    64. gravity: 'center'
    65. })
    66. },
    67. confirm() {
    68. prompt.showToast({
    69. message: '搜索',
    70. duration: "100000",
    71. gravity: 'center'
    72. })
    73. }
    74. }
    75. script>
    76. <style>
    77. .container {
    78. flex: 1;
    79. flex-direction: column;
    80. background-color: #ffffff;
    81. }
    82. .section {
    83. background-color: #afeeee;
    84. margin-top: 20px;
    85. margin-bottom: 20px;
    86. font-size: 30px;
    87. padding: 20px;
    88. width: 100%;
    89. }
    90. .example-body {
    91. flex-direction: row;
    92. padding: 10px;
    93. background-color: #ffffff;
    94. width: 100%;
    95. }
    96. .input-view {
    97. flex-direction: row;
    98. background-color: #f8f8f8;
    99. height: 60px;
    100. border-radius: 15px;
    101. margin-left: 60px;
    102. margin-right: 60px;
    103. line-height: 30px;
    104. }
    105. style>

     

     欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

  • 相关阅读:
    技术类公司与业务类公司转型科技智能公司的异同
    合适的工作就是好工作
    【kafka】使用docker启动kafka
    java计算机毕业设计同德佳苑物业信息源程序+mysql+系统+lw文档+远程调试
    最大正方形的边长长度问题解法
    8-Arm PEG-DBCO,八臂-聚乙二醇-二苯基环辛炔,用于修饰生物分子
    VSCode 配置 C++ 环境
    从零开始的 dbt 入门教程 (dbt core 命令进阶篇)
    https
    致美网页练习完整代码
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/126259101