WeChat アプレット --- コンポーネント通信 --- selectComponent を使用してコンポーネント インスタンスを取得します

WeChat アプレット - コンポーネント通信 - selectComponent を使用してコンポーネント インスタンスを取得します

サブコンポーネント コンポーネント
wxml

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

js

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

1. 親ページを通じて子コンポーネントのデータ値を増やします。

親ページ ページ

wxml

<mytest count="{
     
     {count}}" bind:sync="syncCount" class="childC" id="cC"></mytest>
<view>{
   
   {count}}</view>
<button bindtap="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
    })
  },

2. 親ページ経由で子コンポーネントを呼び出す方法

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

おすすめ

転載: blog.csdn.net/m0_48546501/article/details/129676894