子组件:js部分
// components/A/A.js
Component({
/**
* 组件的属性列表
*/
properties: {
arrList: {
type: Array,
value: '默认值'
},
},
/**
* 组件的初始数据
*/
data: {
flag1: false,
},
/**
* 组件的方法列表
*/
methods: {
get(e) {
let index = e.currentTarget.dataset.index
// this.triggerEvent('event',this.data.flag)
this.triggerEvent('aa', this.data.flag1)
this.triggerEvent('i',index )
}
}
})
子组件:html部分
<view wx:for="{{arrList}}" wx:key="index" class="a">
<text class="aa {{item.flag?'active':''}}" bindtap="get" data-index="{{index}}">{{item.name}}</text>
</view>
父组件:js部分
Page({
/**
* 页面的初始数据
*/
data: {
arr: [{
name: 1,
flag: false
},
{
name: 2,
flag: false
},
{
name: 3,
flag: false
},
{
name: 4,
flag: false
},
],
a:'',
b:0
},
change(e){
// console.log(e);
this.setData({
a:e.detail,
})
},
ii(i){
// console.log(i);
this.setData({
b:i.detail
})
this.data.arr.forEach(r=>{
r.flag = false
})
// console.log(this.data.b);
this.data.arr[this.data.b].flag=true
this.setData({
arr:this.data.arr
})
console.log(this.data.arr);
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
父组件:json部分
{
"usingComponents": {
"A":"/components/A/A"
}
}
父组件:html部分
<A arrList = "{{arr}}" bindaa="change" bindi="ii"></A>