• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小码哥-倩倩
博客园    首页    新随笔    联系   管理    订阅  订阅

子组件与父组件之间的

父组件向子组件传值

json

{
  "usingComponents": {
    "reply": "../../components/reply/reply"
  }
}

html

<!-- 评论回复 -->
<reply commentCount='{{commentList.length}}' commentAvatarUrl='{{commentAvatarUrl}}' bind:oneLevelComment='oneLevelCommentSend'></reply>
  • 子组件

js

/**
 * 组件的属性列表
 */
properties: {
  commentCount: {
    type: Number,
    value: 0,
  },
  commentItem: {
    type: Object,
    value: ''
  }
},

然后,在子组件方法中通过 this.data.commentCount 来获取数值

/**
 * 组件挂载后执行
 */
ready: function () {

  // 判断评论内容是否为空
  if (this.data.commentCount > 0) {
    this.setData({
      isCommentEmpty: false
    });
  } else {
    this.setData({
      isCommentEmpty: true
    });
  }
},

二、子组件向父组件传值

需要手动触发获取

  • 子组件

html

<image class='comment-comment' src='../../images/comment_comment.png' bindtap='twoLevelCommentBtnClick' data-author-name="{{commentItem.AuthorName}}"></image>

js

/**
 * 组件的方法列表
 */
methods: {
  // 点击评论按钮
  twoLevelCommentBtnClick: function (e) {
    let authorName = e.currentTarget.dataset.authorName;
    this.triggerEvent("twoLevelCommentBtn", authorName);
  },
},
  • 父组件

html
通过 bind:twoLevelCommentBtn='twoLevelCommentBtnClick' 把子组件的事件传递给父组件的自定义事件

<!-- 评论内容 -->
<block wx:for="{{commentList}}" wx:key="{{index}}">
  <comment commentCount='{{commentList.length}}' commentItem='{{item}}' bind:twoLevelCommentBtn='twoLevelCommentBtnClick' bind:twoLevelCommentCell='twoLevelCommentCellClick'></comment>
</block>

js

// 二级评论按钮点击
twoLevelCommentBtnClick (e) {
  this.setData({
    placeholderInput: e.detail
  });
  consoleUtil.log("点击二级评论按钮:" + e.detail);
},

 

posted @ 2019-04-18 10:51  小码哥-倩倩  阅读(145)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3