微信小程序框架wepy.js父子组件双向通信

props

father.wpy

<style lang="less">
  .userinfo {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .userinfo-avatar {
    width: 80rpx;
    height: 80rpx;
    border-radius: 50%;
  }

  .userinfo-nickname {
    color: #aaa;
  }
</style>
<template>
  <view class="container">

    //.sync表示子组件里更改了twoWayTitle, 父组件里的mynum也会跟着改变 , 双向通信
    <child title="parentTitle999" :addProductImg="addProductImg" :twoWayTitle.sync='mynum' :items="item"></child>

    <view>{{ mynum }}</view>

  </view>
</template>

<script>
  import wepy from 'wepy'
  import child from '../components/child'

  export default class Index extends wepy.page {
    config = {
      navigationBarTitleText: 'test'
    }
    components = {
      child:child
    }

    mixins = [testMixin]

    data = {
      item:{
        name:'tom',
        addr:'usa',
        age:22
      },
      mynum: 20,
      addProductImg:[
        '../assets/product/aceeno.png',
        '../assets/product/boots.png',
        '../assets/product/boots2.png',
        '../assets/product/luca.png'
      ]
    }

    computed = {

    }

    methods = {

    }

    events = {

    }

    onLoad() {

    }
  }
</script>
  • child.wpy
<style type="less">
  .group {

  }
</style>
<template> 
  <view class="group">
    <view>props</view>
    <view>{{ title }}</view>
    <view>{{ twoWayTitle }}</view>
    <button @tap.stop="getProps">child-getProps</button>
  </view>
</template>
<script>
  import wepy from 'wepy'

  export default class child extends wepy.component {
    props = {
        title: String,
        syncTitle: {
            type: String,  //限制类型为字符 
            default: 'null'   //默认值为null
        },
        twoWayTitle: {
            type: Number,   //限制类型为数字
            default: 50,   //默认值为50
            twoWay: true   //true表示在子组件里更改了twoWayTitle值, 父组件也会同步更改
        },
        addProductImg:{
          type:Object,  //必须要指定类型
          default:null
        }
    };

    components = {

    }
    methods = {
      getProps(){
        var num = this.twoWayTitle  //获取props值
        this.twoWayTitle = ++num  
      }
    }
  }
</script>

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值