• 【微信小程序开发】自定义组件以及页面布局设计


    🥳🥳Welcome Huihui's Code World ! !🥳🥳

    接下来看看由辉辉所写的关于小程序的相关操作吧

    目录

    🥳🥳Welcome Huihui's Code World ! !🥳🥳

    一.自定义组件的使用步骤(附实例)

    1.创建自定义组件

    2.在wxml中制定组件模板

    3.wxss中给模板设计样式

    4.js中定义组件的属性

    5.定义组件的相关事件

    6.在其他页面中使用组件

    二.个人中心的布局实现

    wxml

    wxss

    效果预览

    三.投票的布局设计 

    wxml

    wxss

    js

    效果预览


    一.自定义组件的使用步骤(附实例)

    1.创建自定义组件

    但我们如果使用的是最新版的微信开发工具,那么在我们新建了自定义组件之后,会出现一个错误

    这个是因为它在过滤检查文件,如何可能会出现错误,为了不干扰我们的开发,我们需要在project.config.json中写两行代码

    1. "ignoreDevUnusedFiles": false,
    2. "ignoreUploadUnusedFiles": false,

    2.在wxml中制定组件模板

    1. <view class="tabs">
    2. <view class="tabs_title">
    3. <view wx:for="{{tabList}}" wx:key="id" class="title_item {{index==tabIndex?'item_active':''}}" bindtap="handleItemTap" data-index="{{index}}">
    4. <view style="margin-bottom:5rpx">{{item}}view>
    5. <view style="width:30px" class="{{index==tabIndex?'item_active1':''}}">view>
    6. view>
    7. view>
    8. <view class="tabs_content">
    9. <slot>slot>
    10. view>
    11. view>

    3.wxss中给模板设计样式

    1. /* components/tabs/tabs.wxss */
    2. .tabs {
    3. position: fixed;
    4. top: 0;
    5. width: 100%;
    6. background-color: #fff;
    7. z-index: 99;
    8. border-bottom: 1px solid #efefef;
    9. padding-bottom: 20rpx;
    10. }
    11. .tabs_title {
    12. /* width: 400rpx; */
    13. width: 90%;
    14. display: flex;
    15. font-size: 9pt;
    16. padding: 0 20rpx;
    17. }
    18. .title_item {
    19. color: #999;
    20. padding: 15rpx 0;
    21. display: flex;
    22. flex: 1;
    23. flex-flow: column nowrap;
    24. justify-content: center;
    25. align-items: center;
    26. }
    27. .item_active {
    28. /* color:#ED8137; */
    29. color: #000000;
    30. font-size: 11pt;
    31. font-weight: 800;
    32. }
    33. .item_active1 {
    34. /* color:#ED8137; */
    35. color: #000000;
    36. font-size: 11pt;
    37. font-weight: 800;
    38. border-bottom: 6rpx solid #333;
    39. border-radius: 2px;
    40. }

    4.js中定义组件的属性

    5.定义组件的相关事件

    1. // components/tabs/tabs.js
    2. Component({
    3. /**
    4. * 组件的属性列表
    5. */
    6. properties: {
    7. tabList:Object
    8. },
    9. /**
    10. * 组件的初始数据
    11. */
    12. data: {
    13. },
    14. /**
    15. * 组件的方法列表
    16. */
    17. methods: {
    18. handleItemTap(e){
    19. // 获取索引
    20. const {index} = e.currentTarget.dataset;
    21. // 触发 父组件的事件
    22. this.triggerEvent("tabsItemChange",{index})
    23. this.setData({
    24. tabIndex:index
    25. })
    26. }
    27. }
    28. })

    6.在其他页面中使用组件

    ①在对应使用的界面的json中引用自定义组件

    1. {
    2. "usingComponents": { "tabs":"/components/tabs/tabs"},
    3. "navigationBarTitleText": "会议"
    4. }

    ②在使用组件的wxml页面中使用组件

    "{{tabs}}"  bindtabsItemChange="tabsItemChange">

    ③定义属性值

    1. Page({
    2. /**
    3. * 页面的初始数据
    4. */
    5. data: {
    6. tabs:['会议中','已完成','已取消','全部会议'],
    7. lists: [
    8. {
    9. 'id': '1',
    10. 'image': '/static/persons/1.jpg',
    11. 'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
    12. 'num':'304',
    13. 'state':'进行中',
    14. 'time': '10月09日 17:59',
    15. 'address': '深圳市·南山区'
    16. },
    17. {
    18. 'id': '1',
    19. 'image': '/static/persons/2.jpg',
    20. 'title': 'AI WORLD 2016世界人工智能大会',
    21. 'num':'380',
    22. 'state':'已结束',
    23. 'time': '10月09日 17:39',
    24. 'address': '北京市·朝阳区'
    25. },
    26. {
    27. 'id': '1',
    28. 'image': '/static/persons/3.jpg',
    29. 'title': 'H100太空商业大会',
    30. 'num':'500',
    31. 'state':'进行中',
    32. 'time': '10月09日 17:31',
    33. 'address': '大连市'
    34. },
    35. {
    36. 'id': '1',
    37. 'image': '/static/persons/4.jpg',
    38. 'title': '报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”',
    39. 'num':'150',
    40. 'state':'已结束',
    41. 'time': '10月09日 17:21',
    42. 'address': '北京市·朝阳区'
    43. },
    44. {
    45. 'id': '1',
    46. 'image': '/static/persons/5.jpg',
    47. 'title': '新质生活 · 品质时代 2016消费升级创新大会',
    48. 'num':'217',
    49. 'state':'进行中',
    50. 'time': '10月09日 16:59',
    51. 'address': '北京市·朝阳区'
    52. }
    53. ],
    54. lists1: [
    55. {
    56. 'id': '1',
    57. 'image': '/static/persons/1.jpg',
    58. 'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
    59. 'num':'304',
    60. 'state':'进行中',
    61. 'time': '10月09日 17:59',
    62. 'address': '深圳市·南山区'
    63. },
    64. {
    65. 'id': '1',
    66. 'image': '/static/persons/2.jpg',
    67. 'title': 'AI WORLD 2016世界人工智能大会',
    68. 'num':'380',
    69. 'state':'已结束',
    70. 'time': '10月09日 17:39',
    71. 'address': '北京市·朝阳区'
    72. },
    73. {
    74. 'id': '1',
    75. 'image': '/static/persons/3.jpg',
    76. 'title': 'H100太空商业大会',
    77. 'num':'500',
    78. 'state':'进行中',
    79. 'time': '10月09日 17:31',
    80. 'address': '大连市'
    81. }
    82. ],
    83. lists2: [
    84. {
    85. 'id': '1',
    86. 'image': '/static/persons/1.jpg',
    87. 'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
    88. 'num':'304',
    89. 'state':'进行中',
    90. 'time': '10月09日 17:59',
    91. 'address': '深圳市·南山区'
    92. },
    93. {
    94. 'id': '1',
    95. 'image': '/static/persons/2.jpg',
    96. 'title': 'AI WORLD 2016世界人工智能大会',
    97. 'num':'380',
    98. 'state':'已结束',
    99. 'time': '10月09日 17:39',
    100. 'address': '北京市·朝阳区'
    101. }
    102. ],
    103. lists3: [
    104. {
    105. 'id': '1',
    106. 'image': '/static/persons/1.jpg',
    107. 'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
    108. 'num':'304',
    109. 'state':'进行中',
    110. 'time': '10月09日 17:59',
    111. 'address': '深圳市·南山区'
    112. },
    113. {
    114. 'id': '1',
    115. 'image': '/static/persons/2.jpg',
    116. 'title': 'AI WORLD 2016世界人工智能大会',
    117. 'num':'380',
    118. 'state':'已结束',
    119. 'time': '10月09日 17:39',
    120. 'address': '北京市·朝阳区'
    121. },
    122. {
    123. 'id': '1',
    124. 'image': '/static/persons/3.jpg',
    125. 'title': 'H100太空商业大会',
    126. 'num':'500',
    127. 'state':'进行中',
    128. 'time': '10月09日 17:31',
    129. 'address': '大连市'
    130. },
    131. {
    132. 'id': '1',
    133. 'image': '/static/persons/4.jpg',
    134. 'title': '报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”',
    135. 'num':'150',
    136. 'state':'已结束',
    137. 'time': '10月09日 17:21',
    138. 'address': '北京市·朝阳区'
    139. },
    140. {
    141. 'id': '1',
    142. 'image': '/static/persons/5.jpg',
    143. 'title': '新质生活 · 品质时代 2016消费升级创新大会',
    144. 'num':'217',
    145. 'state':'进行中',
    146. 'time': '10月09日 16:59',
    147. 'address': '北京市·朝阳区'
    148. }
    149. ]
    150. },
    151. /**
    152. * 生命周期函数--监听页面加载
    153. */
    154. onLoad(options) {
    155. },
    156. /**
    157. * 生命周期函数--监听页面显示
    158. */
    159. onShow() {
    160. },
    161. tabsItemChange(e){
    162. let tolists;
    163. if(e.detail.index==1){
    164. tolists = this.data.lists1;
    165. }else if(e.detail.index==2){
    166. tolists = this.data.lists2;
    167. }else{
    168. tolists = this.data.lists3;
    169. }
    170. this.setData({
    171. lists: tolists
    172. })
    173. }
    174. })

    效果预览

    二.个人中心的布局实现

    wxml

    1. <view class="userInfo">
    2. <image class="user-profile" src="/static/persons/1.jpg">image>
    3. <text class="user-name">用户登录text>
    4. <text class="user-click">修改text>
    5. view>
    6. <view class="item1">
    7. <view class="box1">
    8. <image src="/static/tabBar/sdk.png" class="box-icon">image>
    9. <text class="box-title">我主持的会议text>
    10. <text class="box-num">1text>
    11. <text class="box-click">text>
    12. view>
    13. <view class="box2">
    14. <image src="/static/tabBar/sdk.png" class="box-icon">image>
    15. <text class="box-title">我参与的会议text>
    16. <text class="box-num">1000text>
    17. <text class="box-click">text>
    18. view>
    19. view>
    20. <view class="item2">
    21. <view class="box1">
    22. <image src="/static/tabBar/sdk.png" class="box-icon">image>
    23. <text class="box-title">我发布的投票text>
    24. <text class="box-num">12text>
    25. <text class="box-click">text>
    26. view>
    27. <view class="box2">
    28. <image src="/static/tabBar/sdk.png" class="box-icon">image>
    29. <text class="box-title">我参与的投票text>
    30. <text class="box-num">100text>
    31. <text class="box-click">text>
    32. view>
    33. view>
    34. <view class="item3">
    35. <view class="box1">
    36. <image src="/static/tabBar/sdk.png" class="box-icon">image>
    37. <text class="box-title">消息text>
    38. <text class="boxs-click">text>
    39. view>
    40. <view class="box2">
    41. <image src="/static/tabBar/sdk.png" class="box-icon">image>
    42. <text class="box-title">设置text>
    43. <text class="boxs-click">text>
    44. view>
    45. view>

    wxss

    1. /* pages/ucenter/index/index.wxss */
    2. .userInfo{
    3. height:100px;
    4. display: flex;
    5. border-bottom: rgb(235, 234, 234) solid 6px;
    6. }
    7. .user-profile{
    8. width: 80px;
    9. height: 80px;
    10. display: flex;
    11. align-items: center;
    12. }
    13. .user-name{
    14. font-weight: bolder;
    15. display: flex;
    16. align-items: center;
    17. margin-left: 20px;
    18. width: 450rpx;
    19. }
    20. .user-click{
    21. display: flex;
    22. align-items: center;
    23. color: lightgray;
    24. }
    25. .item1{
    26. /* /* display: flex; */
    27. border-bottom: rgb(235, 234, 234) solid 6px;
    28. }
    29. .box1{
    30. height: 50px;
    31. display: flex;
    32. align-items: center;
    33. border-bottom: rgb(235, 234, 234) solid 1px;
    34. }
    35. .box2{
    36. height: 50px;
    37. display: flex;
    38. align-items: center;
    39. }
    40. .box-icon{
    41. width: 50rpx;
    42. height: 50rpx;
    43. margin-right: 10px;
    44. }
    45. .box-title{
    46. }
    47. .box-num{
    48. width: 20px;
    49. margin-left: 180px;
    50. }
    51. .box-click{
    52. margin-left: 20px;
    53. color: lightgray;
    54. }
    55. .item2{
    56. /* /* display: flex; */
    57. border-bottom: rgb(235, 234, 234) solid 6px;
    58. }
    59. .boxs-click{
    60. margin-left: 282px;
    61. color: lightgray;
    62. }

    效果预览

    三.投票的布局设计 

    wxml

    1. <view class="info-title">
    2. <image class="img-title" src="../../../static/vote/sousuo.png">image>
    3. <input type="text" placeholder="选择所属会议" />
    4. view>
    5. <view class="vote-button" bindtap="jumpToFoodPage">
    6. <block wx:for="{{myvotelist}}" wx:key="index">
    7. <view class="myvotes">
    8. <view class="myimg">
    9. <image class="vote-icon" src="{{item.img}}">image>
    10. view>
    11. <view class="myview">
    12. <text class="vote-label">{{item.name}}text>
    13. view>
    14. view>
    15. block>
    16. <image class="vote-add" src="../../../static/vote/addvote.png">image>
    17. view>

    wxss

    1. // pages/vote/list/list.js
    2. Page({
    3. /**
    4. * 页面的初始数据
    5. */
    6. data: {
    7. myvotelist:[
    8. {index:1, name:'投票统计',img:'../../../static/vote/totaldata-active.png'},
    9. {index:3, name:'历史投票',img:'../../../static/vote/voterecords-active.png'},
    10. {index:2, name:'赞成人数',img:'../../../static/vote/yes-active.png'},
    11. {index:3, name:'反对人数',img:'../../../static/vote/no-active.png'},
    12. ]
    13. },
    14. /**
    15. * 生命周期函数--监听页面加载
    16. */
    17. onLoad(options) {
    18. },
    19. /**
    20. * 生命周期函数--监听页面初次渲染完成
    21. */
    22. onReady() {
    23. },
    24. /**
    25. * 生命周期函数--监听页面显示
    26. */
    27. onShow() {
    28. },
    29. /**
    30. * 生命周期函数--监听页面隐藏
    31. */
    32. onHide() {
    33. },
    34. /**
    35. * 生命周期函数--监听页面卸载
    36. */
    37. onUnload() {
    38. },
    39. /**
    40. * 页面相关事件处理函数--监听用户下拉动作
    41. */
    42. onPullDownRefresh() {
    43. },
    44. /**
    45. * 页面上拉触底事件的处理函数
    46. */
    47. onReachBottom() {
    48. },
    49. /**
    50. * 用户点击右上角分享
    51. */
    52. onShareAppMessage() {
    53. }
    54. })

    js

    1. // pages/vote/list/list.js
    2. Page({
    3. /**
    4. * 页面的初始数据
    5. */
    6. data: {
    7. myvotelist:[
    8. {index:1, name:'投票统计',img:'../../../static/vote/totaldata-active.png'},
    9. {index:3, name:'投票记录',img:'../../../static/vote/voterecords-active.png'},
    10. {index:2, name:'赞成人数',img:'../../../static/vote/yes-active.png'},
    11. {index:3, name:'反对人数',img:'../../../static/vote/no-active.png'},
    12. ]
    13. },
    14. /**
    15. * 生命周期函数--监听页面加载
    16. */
    17. onLoad(options) {
    18. },
    19. /**
    20. * 生命周期函数--监听页面初次渲染完成
    21. */
    22. onReady() {
    23. },
    24. /**
    25. * 生命周期函数--监听页面显示
    26. */
    27. onShow() {
    28. },
    29. /**
    30. * 生命周期函数--监听页面隐藏
    31. */
    32. onHide() {
    33. },
    34. /**
    35. * 生命周期函数--监听页面卸载
    36. */
    37. onUnload() {
    38. },
    39. /**
    40. * 页面相关事件处理函数--监听用户下拉动作
    41. */
    42. onPullDownRefresh() {
    43. },
    44. /**
    45. * 页面上拉触底事件的处理函数
    46. */
    47. onReachBottom() {
    48. },
    49. /**
    50. * 用户点击右上角分享
    51. */
    52. onShareAppMessage() {
    53. }
    54. })

    效果预览

    好啦,今天的分享就到这了,希望能够帮到你呢!😊😊  

  • 相关阅读:
    RCE 远程代码执行漏洞分析
    ESP32网络开发实例-将数据保存到InfluxDB时序数据库
    IP地址划分知识点总结
    <算法>贪心策略设计并解决会场安排问题
    1004 Counting Leaves
    【k8s源码篇之Informer篇4】关于 Informer 的一些困惑点
    vue3中函数必须有返回值么?
    QTableWidget 详细使用指南
    记一次问题排查
    SpringCloud Alibaba之Sentinel配置
  • 原文地址:https://blog.csdn.net/m0_74315688/article/details/133902665