小程序實現(xiàn)頁面給自定義組件賦值
本文實例為大家分享了小程序之頁面給自定義組件賦值的具體代碼,供大家參考,具體內容如下
1.新建組件:在component下新建一個tabBar
2.組件中的index.wxml結構如下:
<cover-view class="tab-bar"> ?? ?<cover-view class="tab-bar-border"></cover-view> ?? ?<cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="tabChange"> ?? ??? ?<cover-image src="{{tabbarIndex === index ? item.selectedIconPath : item.iconPath}}"></cover-image> ?? ??? ?<cover-view style="color: {{tabbarIndex === index ? selectedColor : color}}">{{item.text}}</cover-view> ?? ?</cover-view> </cover-view>
3.組件中的index.js結構如下:
Component({ ? /** ?1. 組件的屬性列表 ? ?*/ ? options: { ? ? multipleSlots: true //在組件定義時的選項中啟用多slot支持 ? }, ? properties: { ? ? list: {// 屬性名 ? ? ? type: Array, ? ? ? value: [] ? ? }, ? ? selectedColor:{// 屬性名 ? ? ? type: String, ? ? ? value:'' ? ? }, ? ? color:{// 屬性名 ? ? ? type: String, ? ? ? value:'' ? ? }, ? }, ? /** ?2. 組件的初始數(shù)據(jù) ? ?*/ ? data: { ? ? tabbarIndex: 0//默認顯示第一個tab元素 ? }, ? lifetimes: { ? ? attached() {} ? }, ? /** ?3. 組件的方法列表 ? ?*/ ? methods: { ? ? //組件的點擊事件 ? ? tabChange(e) { ? ? ? //獲取到底部欄元素的下標 ? ? ? let index = e.currentTarget.dataset.index; ? ? ? this.setData({ ? ? ? ? tabbarIndex:index, ? ? ? }) ? } })
4.組件中的index.json結構如下:
{ ? "component": true, ? "usingComponents": {} }
5.組件的引用:在頁面pages/index/index.json中加入
{ ? "navigationBarTitleText": "測試", ? "usingComponents": { ? ? "mp-tabbar": "../components/tabBar/index" ? } }
6.在頁面pages/index/index.wxml中加入
<view wx:if="{{tabbarIndex == 0}}">111111</view> <view wx:if="{{tabbarIndex == 1}}">222222</view> <view wx:if="{{tabbarIndex == 2}}">333333</view> <mp-tabbar list="{{list}}" id='tabComponent' bind:onMyEvent="switchTab"></mp-tabbar>
7.在頁面pages/index/index.js中加入
data: { ? ? tabbarIndex:0,//默認顯示市場 ? ? color: "#555555", ? ? selectedColor: "#2ea7e0", ? ? //底部欄 ? ? items: [{ ? ? ? ? "text": "市場", ? ? ? ? "iconPath": "/images/bazaar.png", ? ? ? ? "selectedIconPath": "/images/tselected.png", ? ? ? }, ? ? ? { ? ? ? ? "text": "充值", ? ? ? ? "iconPath": "/images/recharge.png", ? ? ? ? "selectedIconPath": "/images/recharge_selected.png", ? ? ? }, { ? ? ? ? "text": "車隊", ? ? ? ? "iconPath": "/images/market.png", ? ? ? ? "selectedIconPath": "/images/market_selected.png", ? ? ? } ? ? ] ? }, ? onShow: function () { ? ? this.tabComponent = this.selectComponent('#tabComponent'); ? ? let selectedColor = this.data.selectedColor; ? ? let color = this.data.color; ? ? this.tabComponent.setData({ ? ? ? selectedColor: selectedColor, ? ? ? color:color ? ?}) ? ?console.log(this.tabComponent.data.tabbarIndex) ? },
8.最終效果如圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用uniapp打包微信小程序時主包和vendor.js過大解決(uniCloud的插件分包)
每個使用分包小程序必定含有一個主包,所謂的主包,即放置默認啟動頁面/TabBar頁面,以及一些所有分包都需用到公共資源/JS 腳本,下面這篇文章主要給大家介紹了關于使用uniapp打包微信小程序時主包和vendor.js過大解決的相關資料,,需要的朋友可以參考下2023-02-02JavaScript新功能介紹之findLast()和findLastIndex()
最近工作中遇到了一個關于查找數(shù)組里面的目標元素的方法,所以下面這篇文章主要給大家介紹了關于JavaScript新功能之findLast()?和findLastIndex()的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04JavaScript數(shù)組Array的一些常用方法總結
JavaScript的Array對象是用于構造數(shù)組的全局對象,數(shù)組是類似于列表的高階對象,下面這篇文章主要給大家介紹了關于JavaScript數(shù)組Array的一些常用方法,需要的朋友可以參考下2021-11-11