找回密码
 立即注册

微信小程序进阶

匿名  发表于 2023-3-10 08:50:56 阅读模式 打印 上一主题 下一主题
前言:
在开辟微信小法式的进程中 微信小法式的底部与顶部默许自带的款式并不能满足平常所需,那末关于自界说顶部与自界说底部的需求也就来了。
1.自界说顶部  navigation

1.1 全局自界说顶部
一切的页面 均利用自界说 顶部,自带顶部全局设置生效    app.json
  1. "window": {
  2.     "backgroundTextStyle": "light",
  3.     "navigationBarBackgroundColor": "#fff",
  4.     "navigationBarTitleText": "Weixin",
  5.     "navigationBarTextStyle": "black",
  6.      //全局自界说顶部
  7.     "navigationStyle": "custom"   
  8.   },
复制代码
1.2 单个页面自界说顶部
针对单个页面的自界说顶部设置   页面.json
  1. "navigationStyle": "custom"
复制代码
1.3 建立自界说 top-bar 组件

1.根目录下/components/top-bar 目录中 右键建立 topbar Component组件
2.在父页面 的 json文件中注册 自界说组件
  1. {
  2.   "usingComponents": {
  3.     "top-bar":"/components/top-bar/top-bar"
  4.   },
  5.   "navigationStyle": "custom"
  6. }
复制代码
3.在父页面的 wxml 文件中利用  该组件
  1. <!--index.wxml-->
  2. <view class="container">
  3.   <top-bar title="首页"></top-bar>
  4. </view>
复制代码
4.完善 top-bar 组件款式,肯定其高度与位置信息

  • top-bar.js
  1. // components/top-bar/top-bar.js
  2. Component({
  3.   /**
  4.    * 组件的属性列表
  5.    */
  6.   properties: {
  7.     title:{
  8.       type:String,
  9.       default:'题目'
  10.     }
  11.   },
  12.   /**
  13.    * 组件的初始数据
  14.    */
  15.   data: {
  16.     topBarHeight:0,  //顶部盒子总高度
  17.   },
  18.   /**
  19.    * 组件的方式列表
  20.    */
  21.   methods: {
  22.     getNavHeight(){
  23.         // 获得装备信息
  24.         const sysInfo = wx.getSystemInfoSync();
  25.         // 导航栏总高度 = 状态栏+44px
  26.         const TopBarheight = sysInfo.statusBarHeight+44;
  27.         //异步更新数据
  28.         wx.nextTick(()=>{
  29.           this.setData({
  30.             topBarHeight:TopBarheight,
  31.           })
  32.         })
  33.     }
  34.   },
  35.   created(){
  36.     this.getNavHeight()
  37.   },
  38. })
复制代码

  • top-bar.wxml
  1. <!--components/top-bar/top-bar.wxml-->
  2. <view class="nav" style="height:{{topBarHeight}}px;padding: {{topBarHeight}}rpx 0 0 0;">
  3.   <view>
  4.     {{title}}
  5.   </view>
  6. </view>
复制代码

  • top-bar.wxss
  1. /* components/top-bar/top-bar.wxss */
  2. .nav{
  3.   box-sizing: border-box;
  4.   background-color: #f1f1f1;
  5. }
复制代码
5.完整父页面中的设置

  • index.json
  1. {
  2.   "usingComponents": {
  3.     "top-bar":"/components/top-bar/top-bar"
  4.   },
  5.   "navigationStyle": "custom"
  6. }
复制代码

  • index.wxml
  1. <!--index.wxml-->
  2. <view class="container">
  3. <!-- 顶部组件 -->
  4.   <top-bar title="公共点评上首页"></top-bar>
  5.   <!-- 内容地区 -->
  6.   <view class="content" >
  7.       <view  wx:for="{{100}}">
  8.           {{item}}
  9.       </view>
  10.   </view>
  11. </view>
复制代码

  • index.wxss
  1. /**index.wxss**/
  2. .container{
  3.   width: 100vw;
  4.   height: 100vh;
  5.   display: flex;
  6.   flex-direction: column;
  7. }
  8. .content{
  9.   flex: 1;
  10.   width: 100%;
  11.   background-color: #f60;
  12.   overflow-y: scroll;
  13. }
复制代码
6.终极展现结果

微信小法式进阶-1.jpg
2.自界说底部  TabBar

2.1 全局封闭 默许tabBar 设置,开启自界说tabBar
留意: 需要留意的是,就算开启了自界说 tabBar 设置,照旧需要保存全局的 tabBar 的list 设置【这样做是为了 可以经过  wx.switchTab() 方式可以跳转到 该页面,该页面照旧为 tabBar 】
  1. "tabBar": {
  2.     "custom": true,    //开启自界说tabBar  默许tabBar 款式生效
  3.     "color": "#d4d4d2",
  4.     "selectedColor": "#e26c4f",
  5.     "borderStyle": "white",
  6.     "list": [
  7.       {
  8.         "pagePath": "pages/index/index",
  9.         "text": "首页",
  10.         "iconPath": "/images/index.png",
  11.         "selectedIconPath": "/images/index_active.png"
  12.       },
  13.       {
  14.         "pagePath": "pages/add/add",
  15.         "text": "公布",
  16.         "isCenter":true,   //这里是为了表白该子项 是显现在中心的圆里面
  17.         "iconPath": "/images/add.png",
  18.         "selectedIconPath": "/images/add.png"
  19.       },
  20.       {
  21.         "pagePath": "pages/discount/discount",
  22.         "text": "找优惠",
  23.         "iconPath": "/images/discount.png",
  24.         "selectedIconPath": "/images/discount_active.png"
  25.       },
  26.       {
  27.         "pagePath": "pages/shop/shop",
  28.         "text": "找好店",
  29.         "iconPath": "/images/shop.png",
  30.         "selectedIconPath": "/images/shop_active.png"
  31.       },
  32.       {
  33.         "pagePath": "pages/mine/mine",
  34.         "text": "我的",
  35.         "iconPath": "/images/mine.png",
  36.         "selectedIconPath": "/images/mine_active.png"
  37.       }
  38.     ]
  39.   }
复制代码
2.2 建立公共组件  components/tab-bar


  • tab-bar.wxml
  1. <!--components/tab-bar/tab-bar.wxml-->
  2. <view class="bar">
  3.   <block wx:for="{{tabbar.list}}" wx:key="index">
  4.     <!-- 假如是中心的 -->
  5.     <block wx:if="{{item.isCenter}}">
  6.       <view class="center" style="background: {{tabbar.selectedColor}};" data-page="{{item.pagePath}}" bindtap="toPage">
  7.         <image class="add-icon" src="{{item.iconPath}}" mode="" />
  8.       </view>
  9.     </block>
  10.     <!-- 假如不是中心的 处置 -->
  11.     <block wx:else>
  12.     <!-- 假如不选中 -->
  13.       <view wx:if="{{index!=active}}" bindtap="toPage" data-page="{{item.pagePath}}" class="item" style="color:{{tabbar.color}}">
  14.         <image class="icon" src="{{item.iconPath}}"></image>
  15.         <view>
  16.           {{item.text}}
  17.         </view>
  18.       </view>
  19.       <!-- 假如选中 -->
  20.       <view wx:else class="item" bindtap="toPage" data-page="{{item.pagePath}}" style="color:{{tabbar.selectedColor}}">
  21.         <image class="icon" src="{{item.selectedIconPath}}"></image>
  22.         <view>
  23.           {{item.text}}
  24.         </view>
  25.       </view>
  26.     </block>
  27.   </block>
  28. </view>
复制代码

  • tab-bar.js
这里的data 里面设备了一个默许数据 tabBar 就是间接复制 的 app.json 里面的 tabBar 设置的 json工具
  1. // components/tab-bar/tab-bar.js
  2. Component({
  3.   /**
  4.    * 组件的属性列表
  5.    */
  6.   properties: {
  7.       //当前选中的是哪个tabBar
  8.     active:{
  9.       type:Number,
  10.       default:0,
  11.     }
  12.   },
  13.   /**
  14.    * 组件的初始数据
  15.    */
  16.   data: {
  17.     tabbar:{
  18.       "color": "#d4d4d2",
  19.       "selectedColor": "#e26c4f",
  20.       "borderStyle": "white",
  21.       "list": [
  22.         {
  23.           "pagePath": "pages/index/index",
  24.           "text":"首页",
  25.           "iconPath": "/images/index.png",
  26.           "selectedIconPath": "/images/index_active.png"
  27.         },
  28.         {
  29.           "pagePath": "pages/discount/discount",
  30.           "text":"找优惠",
  31.           "iconPath": "/images/discount.png",
  32.           "selectedIconPath": "/images/discount_active.png"
  33.         },
  34.         {
  35.           "pagePath": "pages/add/add",
  36.           "text": "公布",
  37.           "isCenter":true,
  38.           "iconPath": "/images/add.png",
  39.           "selectedIconPath": "/images/add.png"
  40.         },
  41.         {
  42.           "pagePath": "pages/shop/shop",
  43.           "text":"找好店",
  44.           "iconPath": "/images/shop.png",
  45.           "selectedIconPath": "/images/shop_active.png"
  46.         },
  47.         {
  48.           "pagePath": "pages/mine/mine",
  49.           "text":"我的",
  50.           "iconPath": "/images/mine.png",
  51.           "selectedIconPath": "/images/mine_active.png"
  52.         }
  53.       ]
  54.     }
  55.   },
  56.   /**
  57.    * 组件的方式列表
  58.    */
  59.   methods: {
  60.       //跳转页面
  61.     toPage(event){
  62.       wx.switchTab({
  63.         url: '/'+event.currentTarget.dataset.page
  64.       })
  65.     }
  66.   }
  67. })
复制代码

  • tab-bar.wxss
  1. /* components/tab-bar/tab-bar.wxss */
  2. .bar{
  3.   width: 100%;
  4.   height: 50px;
  5.   background: #fff;
  6.   position: fixed;
  7.   bottom: 0;
  8.   display: flex;
  9.   justify-content: space-around;
  10.   border-top: 1px solid #eee;
  11. }
  12. .item{
  13.   flex: 1;
  14.   height: 50px;
  15.   display: flex;
  16.   flex-direction: column;
  17.   justify-content: center;
  18.   align-items: center;
  19.   font-size: 14px;
  20. }
  21. .icon{
  22.   width: 25px;
  23.   height: 25px;
  24. }
  25. .center{
  26.   position: relative;
  27.   top: -20px;
  28.   width: 50px;
  29.   height: 50px;
  30.   text-align: center;
  31.   line-height: 50px;
  32.   border-radius: 50%;
  33.   color: #fff;
  34. }
  35. .add-icon{
  36.   margin:12.5px;
  37.   width: 25px;
  38.   height: 25px;
  39. }
复制代码
2.3 需要 利用tabBar的页面 导入该子组件

页面.json
  1. {
  2.   "usingComponents": {
  3.     "top-bar":"/components/top-bar/top-bar",
  4.     "tab-bar":"/components/tab-bar/tab-bar"
  5.   }
  6. }
复制代码
页面.wxml
  1. <!--index.wxml-->
  2. <view class="container">
  3. <!-- 顶部组件 -->
  4.   <top-bar title="公共点评上首页"></top-bar>
  5.   <!-- 内容地区 -->
  6.   <view class="content" >
  7.       <!-- <view  wx:for="{{100}}">
  8.           {{item}}
  9.       </view> -->
  10.   </view>
  11. </view>
  12. <!-- 自界说底部  tabBar  2:暗示当前第几个被选中 进入这个页面时,需要选中的数组索引  -->
  13. <tab-bar active="2"></tab-bar>
复制代码
页面.wxss
  1. /**index.wxss**/
  2. .container{
  3.   width: 100vw;
  4.   height: 100vh;
  5.   display: flex;
  6.   flex-direction: column;
  7. }
  8. .content{
  9.   flex: 1;
  10.   width: 100%;
  11.   background-color: #f60;
  12.   overflow-y: scroll;
  13.   padding-bottom: 50px;
  14. }
复制代码
2.4 结果展现


微信小法式进阶-2.jpg
3.外联款式的导入
操纵@import “导入的款式文件相对途径”    可以导入公共的外联款式
  1. @import "../../common/common.wxss";
复制代码
4.WeUI-微信官方团队开辟UI组件库
官网文档链接: https://wechat-miniprogram.github.io/weui/docs/quickstart.html
4.1 下载并在小法式中利用 UI组件库

小法式支持利用npm安装第三方包,要求开辟者工具>=1.02,根本库版本>=2.2.1
  1. npm i weui-miniprogram
  2. yarn add weui-miniprogram
复制代码
4.2 在 app.wxss 中导入 组件库 款式文件
  1. /**app.wxss**/
  2. @import "./miniprogram_npm/weui-miniprogram/weui-wxss/dist/style/weui.wxss";
复制代码
4.3 假如出现 控制台报错:依靠项 未被利用的报错


微信小法式进阶-3.jpg

  • 处理计划: 封闭依靠项检测提醒,并重启项目   project.config.json
  1. "setting": {
  2.     "ignoreDevUnusedFiles": false,
  3.     "ignoreUploadUnusedFiles": false,
  4.   }
复制代码
4.4 利用 weui 组件


  • index.json 注册该组件
  1. {
  2.   "usingComponents": {
  3.     "mp-dialog": "weui-miniprogram/dialog/dialog"
  4.   }
  5. }
复制代码

  • index.wxml
  1. <mp-dialog title="test" show="{{true}}" bindbuttontap="tapDialogButton" buttons="{{buttons}}">
  2.     <view>test content</view>
  3. </mp-dialog>
复制代码

  • index.js
  1. Page({
  2.   data: {
  3.     buttons: [{text: '取消'}, {text: '确认'}]
  4.   }
  5. }
复制代码
完整的组件的利用文档请参考具体的组件的文档。
回复

使用道具

说点什么

您需要登录后才可以回帖 登录 | 立即注册
HOT • 推荐

神回复

站长姓名:王殿武 杭州共生网络科技 创始人 云裂变新零售系统 创始人 飞商人脉对接平台 创始人 同城交友聚会平台 创始人 生活经验分享社区 创始人 合作微信:15924191378(注明来意)