私の連絡先情報
郵便メール:
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
WeChatミニプログラムにカスタマイズを実装する tabBar
アプリケーションに、より柔軟でパーソナライズされた下部ナビゲーション メニューを提供できます。公式WeChatミニプログラムによるtabBar
限られた構成機能、カスタマイズtabBar
多くの開発者は、複雑なボトム ナビゲーションを実装することを選択しています。以下は、ミニ プログラムでカスタマイズを実装する方法の簡単な例です。tabBar
。
まず、次のことを行う必要があります。 app.json
中程度の構成tabBar
基本的な情報ですが、custom
フィールドを次のように設定する必要がありますtrue
カスタマイズを有効にするにはtabBar
。
{
"pages": [
"pages/index/index",
"pages/logs/logs",
"pages/profile/profile"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle": "black"
},
"tabBar": {
"list": [
{
"pagePath": "pages/index/index",
"text": "首页"
},
{
"pagePath": "pages/logs/logs",
"text": "日志"
},
{
"pagePath": "pages/profile/profile",
"text": "我的"
}
],
"custom": true,
"color": "#7A7E83",
"selectedColor": "#3cc51f",
"borderStyle": "black"
}
}
という名前のファイルを作成します custom-tab-bar
フォルダーを作成し、その中に必要なファイルを作成します (例:index.js
、index.json
、index.wxml
、index.wxss
)。
現在のページに基づいてタブを切り替えるなどのロジックをここで処理できます。
Component({
data: {
selected: 0,
list: [{
pagePath: '/pages/index/index',
text: '首页'
}, {
pagePath: '/pages/logs/logs',
text: '日志'
}, {
pagePath: '/pages/profile/profile',
text: '我的'
}]
},
methods: {
switchTab: function(e) {
const data = e.currentTarget.dataset;
if (this.data.selected === data.index) return;
wx.switchTab({url: data.path});
},
onShow: function(e) {
// 当 tabBar 显示时触发,可以根据需要获取当前页面路径
const { path } = getCurrentPages().pop();
this.setData({
selected: this.data.list.findIndex(tab => tab.pagePath === path)
});
}
}
});
コンポーネントの構成情報を定義します。
{
"usingComponents": {}
}
tabBarの構造を定義します。
<view class="tab-bar">
<block wx:for="{{list}}" wx:key="index" wx:for-index="idx">
<view class="tab-item {{selected === idx ? 'active' : ''}}" data-path="{{item.pagePath}}" data-index="{{idx}}" bindtap="switchTab">
{{item.text}}
</view>
</block>
</view>
スタイルを追加します。
.tab-bar {
display: flex;
justify-content: space-around;
background-color: #fff;
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 50px;
}
.tab-item {
padding: 10px 0;
text-align: center;
}
.tab-item.active {
color: #3cc51f;
}
存在する app.js
カスタム tabBar コンポーネントを導入して使用します。
App({
onLaunch: function () {
// 在这里可以执行一些启动时的逻辑
},
usingComponents: {
'不同角色,控制查看底部菜单权限
实现效果1:实体店
实现效果2:回收公司
