0


微信小程序---组件通信---使用selectComponent获取组件实例

微信小程序—组件通信—使用selectComponent获取组件实例

子组件component
wxml

<view>{{count}}</view>

js

  properties: {
	count:Number
  },
  methods: {
	addCount(){
  		this.setData({
    		count:this.properties.count+1
  		})
  	this.triggerEvent('sync',{value:this.properties.count})
	}
  }

一.通过父页面增加子组件的数据值

父页面page

wxml

<mytestcount="{{count}}"bind:sync="syncCount"class="childC"id="cC"></mytest><view>{{count}}</view><buttonbindtap="getChild">获取子组件的实例对象</button>

js

 data: {
	count:1
  },
   syncCount(e){
		this.setData({
  			count:e.detail.value
		})
  },

  getChild(){
    const child = this.selectComponent('.childC')
    child.setData({
      	count:child.properties.count+1
    })
  },

二.通过父页面调用子组件的方法

  getChild(){
    const child = this.selectComponent('.childC')
	child.addCount()
  },


本文转载自: https://blog.csdn.net/m0_48546501/article/details/129676894
版权归原作者 成虚员 所有, 如有侵权,请联系我们删除。

“微信小程序---组件通信---使用selectComponent获取组件实例”的评论:

还没有评论