微信小程序自定义组件在获取到父组件传的值以后如何修改自定义内部数据??
问题描述:
父组件index.wxml stalls是个数组 count的属性是字符串
<view class='stalls' wx:for="{{stalls}}" wx:key=''>
<stars count="{{item.count}}" />
</view>
</view>
自定义组件
stars.wxml
<view class='stall'>
<view>{{count}}</view>
<block wx:for="{{solid}}" wx:key="">
<image src='../../images/star-solid.png'></image>
</block>
<block wx:for="{{half}}" wx:key="">
<image src='../../images/star-half.png'></image>
</block>
<block wx:for="{{hollow}}" wx:key="">
<image src='../../images/star-hollow.png'></image>
</block>
</view>
stars.js
Component({
properties: {
count:String
},
data: {
// 这里是一些组件内部数据
solid: ' ',
half: ' ',
hollow: ' ',
},
onLoad: function () {
this.setData({
solid: parseInt(this.properties.count),
half: Math.ceil(5 - this.aproperties.count) - (5 - self.properties.count),
hollow: parseInt(5 - this.properties.count)
})
},
})
自定义组件内部的data里的数据没有变化
答
第一,组件没有onLoad事件,自己好好看api,改为ready事件
https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/component.html
第二,starts.js代码有问题,properties写成了aproperties,self变量没有定义;乱用
Component({
properties: {
count: String
},
data: {
// 这里是一些组件内部数据
solid: '',
half: '',
hollow: '',
},
ready: function () {
this.setData({
solid: parseInt(this.properties.count),
half: Math.ceil(5 - this.properties.count) - (5 - this.properties.count),
hollow: parseInt(5 - this.properties.count)
})
}
});
答
父组件的值没有传给子组件,你在父组件里面设值就行
答
count="{{item.count}}" 已经传给子组件count的值 我的目的是想使用传过来的count值来设置改变data里{soild,half,hllow}的值
答
我的意见是值没有传到子组件,如果传给了,自然就改变了,在父组件里直接给子组件赋值试试