小程序?qū)崿F(xiàn)頁面給自定義組件賦值
本文實(shí)例為大家分享了小程序之頁面給自定義組件賦值的具體代碼,供大家參考,具體內(nèi)容如下
1.新建組件:在component下新建一個(gè)tabBar
2.組件中的index.wxml結(jié)構(gòu)如下:
<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結(jié)構(gòu)如下:
Component({ ? /** ?1. 組件的屬性列表 ? ?*/ ? options: { ? ? multipleSlots: true //在組件定義時(shí)的選項(xiàng)中啟用多slot支持 ? }, ? properties: { ? ? list: {// 屬性名 ? ? ? type: Array, ? ? ? value: [] ? ? }, ? ? selectedColor:{// 屬性名 ? ? ? type: String, ? ? ? value:'' ? ? }, ? ? color:{// 屬性名 ? ? ? type: String, ? ? ? value:'' ? ? }, ? }, ? /** ?2. 組件的初始數(shù)據(jù) ? ?*/ ? data: { ? ? tabbarIndex: 0//默認(rèn)顯示第一個(gè)tab元素 ? }, ? lifetimes: { ? ? attached() {} ? }, ? /** ?3. 組件的方法列表 ? ?*/ ? methods: { ? ? //組件的點(diǎn)擊事件 ? ? tabChange(e) { ? ? ? //獲取到底部欄元素的下標(biāo) ? ? ? let index = e.currentTarget.dataset.index; ? ? ? this.setData({ ? ? ? ? tabbarIndex:index, ? ? ? }) ? } })
4.組件中的index.json結(jié)構(gòu)如下:
{ ? "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,//默認(rèn)顯示市場 ? ? 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": "車隊(duì)", ? ? ? ? "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.最終效果如圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js獲取json中key所對應(yīng)的value值的簡單方法
下面小編就為大家?guī)硪黄猨s獲取json中key所對應(yīng)的value值的簡單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03使用uniapp打包微信小程序時(shí)主包和vendor.js過大解決(uniCloud的插件分包)
每個(gè)使用分包小程序必定含有一個(gè)主包,所謂的主包,即放置默認(rèn)啟動(dòng)頁面/TabBar頁面,以及一些所有分包都需用到公共資源/JS 腳本,下面這篇文章主要給大家介紹了關(guān)于使用uniapp打包微信小程序時(shí)主包和vendor.js過大解決的相關(guān)資料,,需要的朋友可以參考下2023-02-02checkbox全選所涉及到的知識(shí)點(diǎn)介紹
checkbox全選涉及到的知識(shí)點(diǎn)比如IE里起作用,火狐不起作用,getElementById()與getElementsByName()的區(qū)別等等2013-12-12JavaScript新功能介紹之findLast()和findLastIndex()
最近工作中遇到了一個(gè)關(guān)于查找數(shù)組里面的目標(biāo)元素的方法,所以下面這篇文章主要給大家介紹了關(guān)于JavaScript新功能之findLast()?和findLastIndex()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04JavaScript數(shù)組Array的一些常用方法總結(jié)
JavaScript的Array對象是用于構(gòu)造數(shù)組的全局對象,數(shù)組是類似于列表的高階對象,下面這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)組Array的一些常用方法,需要的朋友可以參考下2021-11-11