微信小程序组件间通信(一)

本文详细介绍了微信小程序中父组件如何向子组件传递值及调用子组件的方法,包括组件属性列表的定义与使用,以及父组件通过selectComponent方法获取并操作子组件。同时,文章还提到了小程序中的小数计算问题、请求超时处理和Canvas隐藏问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、微信小程序父组件向子组件传值

说明:父组件向子组件传值,通过“组件的属性列表”properties

组件属性列表值,是单向绑定,内部修改组件属性不会同步显示

组件外部(父组件)修改组件属性,内部展示跟着修改

使用方式如下

1.组件属性列表定义组件属性

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    innerMsg: {
      type: String,
      value: 'defaultvalue'
    },
    showNum: {
      type: Number,
      value: 100
    }
  },

2.组件中使用、展示属性

<view class='red'>
  {{innerMsg}}
</view>
<view>
  数字:{{showNum}}
</view>

3.父组件设置属性值

<show-two inner-msg='中文内容' show-num='11'></show-two>

显示结果:

二、微信小程序父组件调用子组件定义操作

说明:小程序中使用父组件对象的selectComponent方法,可以根据选择器获取第一个组件对象。

如果获取组件列表可以使用selectAllComponents。

父组件示例代码:

addTwo: function () {
  //根据ID获取组件对象
  var showTwo = this.selectComponent('#myShow');
  //访问属性,使用data访问内部属性和组件属性
  console.info(showTwo.data);
  //执行操作
  showTwo.innerAdd();

  //根据样式获取,建议使用selectAllComponents
  var showThree = this.selectComponent('.myShow');
  console.info(showThree.data);
  showThree.innerAdd();
},

 

更多:

小程序中的小数计算问题/浮点数计算问题

小程序请求超时errMsg : "request:fail socket time out timeout:60000"

微信小程序Canvas隐藏问题处理

转载于:https://my.oschina.net/tianma3798/blog/2247070

### 微信小程序组件间通信的实现方式 微信小程序提供了多种方法来实现组件间通信和数据传递。以下是几种常见的实现方式,包括属性传递、事件传递、全局对象以及状态管理工具。 #### 1. 属性传递(Properties) 父组件可以通过属性的方式向子组件传递数据。子组件通过定义 `properties` 来接收父组件传递的数据[^1]。 ```javascript // 子组件:childComponent.js Component({ properties: { message: { type: String, value: '' } }, methods: { updateMessage(newMessage) { this.setData({ message: newMessage }); } } }); ``` 在父组件中,可以通过 `wxml` 文件将数据传递给子组件: ```html <!-- 父组件:parentComponent.wxml --> <child-component message="{{parentMessage}}"></child-component> ``` #### 2. 事件传递(Events) 子组件可以通过触发自定义事件的方式向父组件传递数据。父组件可以监听这些事件并处理传递过来的数据[^3]。 ```javascript // 子组件:childComponent.js Component({ methods: { sendDataToParent() { this.triggerEvent('customEvent', { data: 'Data from child' }); } } }); ``` 在父组件中,可以通过绑定事件来接收子组件传递的数据: ```html <!-- 父组件:parentComponent.wxml --> <child-component bindcustomEvent="handleCustomEvent"></child-component> ``` ```javascript // 父组件:parentComponent.js Page({ handleCustomEvent(e) { console.log(e.detail.data); // 输出:Data from child } }); ``` #### 3. 全局对象(App 对象) 通过小程序的全局对象 `App`,可以在不同页面或组件之间共享数据[^2]。 ```javascript // app.js App({ globalData: { sharedData: 'Shared data' } }); ``` 在任何页面或组件中都可以访问全局数据: ```javascript // 页面或组件 const app = getApp(); console.log(app.globalData.sharedData); // 输出:Shared data ``` #### 4. 状态管理工具 对于复杂的应用场景,可以使用状态管理工具(如 Redux 或 MobX)来管理全局状态,从而实现组件间的数据共享。 --- ### 示例代码 以下是个完整的示例,展示如何通过属性和事件实现组件间通信: ```javascript // 子组件:childComponent.js Component({ properties: { message: { type: String, value: '' } }, methods: { sendDataToParent() { this.triggerEvent('customEvent', { data: 'Data from child' }); } } }); ``` ```html <!-- 子组件:childComponent.wxml --> <view>{{message}}</view> <button bindtap="sendDataToParent">Send Data</button> ``` ```html <!-- 父组件:parentComponent.wxml --> <child-component message="{{parentMessage}}" bindcustomEvent="handleCustomEvent"></child-component> ``` ```javascript // 父组件:parentComponent.js Page({ data: { parentMessage: 'Hello from parent' }, handleCustomEvent(e) { console.log(e.detail.data); // 输出:Data from child } }); ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值