wepy 父调用子组件方法_微信小程序wepy框架中页面page(父)与组件component(子)之间互相动态传值...

官网上描述:

9829d615bc28a6935d17438efdac52a7.png

在index.page(父)页面与test.component(子)互相动态传值,使用.sync:

canshu1为父传到子的值,canshu2为子传到父的值

父:canshu1在父页面通过点击testt方法动态变化,canshu2在父页面使用watch监听改变

{{item}}

rr{{canshu1}} {{canshu2}}

data = {

canshu1:'55',

canshu2:0

};

watch = {

canshu2 (newValue, oldValue) {

console.log(`canshu2: ${oldValue} -> ${newValue}`)

}

};

methods = {

testt: function(type){

this.canshu1 = type;

}

};

在test.component组件中使用props接收,

子:使用watch动态监听canshu1变化,canshu2通过getProps方法动态变化:

test.component{{canshu}}

点击我

props = {

canshu1: {

type: String, //限制类型为字符

default: null //默认值为null

},

canshu2: {

type: Number, //限制类型为数字

default: 0, //默认值为0

twoWay: true //true表示在子组件里更改了canshu2值, 父组件也会同步更改

},

};

watch = {

canshu1 (newValue, oldValue) {

console.log(`canshu1: ${oldValue} -> ${newValue}`)

}

};

methods = {

getProps(){

var num = this.canshu2 //获取props值

this.canshu2 = ++num

}

};

结果:

6ca1cdcf8304b56eb3ea50f6d0e36aeb.png

注意这里有一个坑:在微信开发工具的预览模式下:

7e28a5fbddf1c9e8b5ddacb84c91085d.png

minified必须为false,否则父子之间的动态传值将失效。

另外wepy仅支持小程序的生命周期:onLoad、onReady函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值