多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
![](https://box.kancloud.cn/011807262f48a4193ab79dd5a4f16b0a_370x491.gif) //index.js //获取应用实例 var app = getApp() Page({ data: { switchData: [ { id: 1, color: 'darkorange', isOn: false }, { id: 2, color: 'darksalmon', isOn: true }, { id: 3, isOn: false, }, { id: 4, color: 'firebrick', isOn: true } ], }, //事件处理函数 bindViewTap: function() { wx.navigateTo({ url: '../logs/logs' }) }, onLoad: function () { console.log('onLoad') var that = this //调用应用实例的方法获取全局数据 app.getUserInfo(function(userInfo){ //更新数据 that.setData({ userInfo:userInfo }) }) }, tapSwitch: function(event) { var index = event.currentTarget.id - 1; this.data.switchData[index].isOn = !this.data.switchData[index].isOn this.setData({ switchData: this.data.switchData }); } }) index.wxml <!--index.wxml--> <import src="../../utils/templates/switch/switch.wxml" /> <view class="container"> <text class="title">自定义switch</text> <view class="switch-container"> <view style="margin-right:20px" wx:for="{{switchData}}"> <template is="switch" data="{{isOn: item.isOn, id:item.id, onTintColor:item.color}}"/> </view> </view> <text class="title">微信switch</text> <view class="switch-container"> <switch/> </view> </view> index.wxss /**index.wxss**/ @import "../../utils/templates/switch/switch.wxss"; .title { font-size: 15; margin-bottom: 5px; color: gray; } .switch-container { display: flex; display: -webkit-flex; margin-bottom: 200rpx; }