创新互联百度小程序教程:comment-list 评论列表组件

  • comment-list 评论列表组件
    • 属性说明
      • comment-param 服务参数说明
      • toolbar-config 参数说明
      • view-more-style 参数说明
      • 代码示例 1:列表组件放入页面
      • 代码示例 2:列表支持折叠
    • 调起评论发布功能
      • 代码示例:列表组件放入浮层且自定义底部 toolbar
    • Bug & Tip

    comment-list 评论列表组件

    解释:评论列表,评论的相关数据为托管数据。
    Web 态说明:由于浏览器的限制,在 Web 态内暂不支持发布评论、收藏、分享等功能。

    属性说明

    属性名类型必填默认值说明
    comment-paramObject评论核心参数
    toolbar-configObject底部 toolbar 的相关配置
    is-page-scrollBooleantrue滚动方式为页面滚动,若组件作为浮层使用,该参数需设为 false
    need-toolbarBooleantrue是否需要底部 toolbar ,若使用开发者自定义的底部 toolbar ,该参数需设为 false
    add-commentBooleanfalse用于调起评论发布器发布评论
    detail-pathString点击单条评论跳转的详情页页面 path ,若没有配置则不会发生跳转;配置的前提是列表与详情均是页面级
    is-foldedBooleanfalse是否折叠列表,默认全展示
    fold-numNumber3折叠后列表展示最大条数,默认 3 条,最多 10 条
    view-more-pathString传入放置评论组件的页面路径,如‘/pages/list/index’,组件内部会触发跳转逻辑
    view-more-styleObject『全部 xx 条』的样式,目前只支持开发者自定义字体颜色
    bindclickcommentEventHandler绑定点击单条评论的事件,点击单条评论时触发,返回数据为{status, data:{srid}}
    bindviewmoreEventHandle绑定点击更多事件,若除了页面跳转还需要其他操作,可通过该回调执行;若为浮层,也可使用该回调自定义交互逻辑

    comment-param 服务参数说明

    属性名类型必填默认值说明示例值
    snidString文章的唯一标识,与 path 参数一一对应‘20200101’
    titleString文章标题
    pathString智能小程序内页链接,最长不能超过 194 字符。如该文章需要入信息流投放,需保证该参数与信息流投放提交的 path 一致,否则将会影响流量“path”:”/pages/index/index”
    imagesArray数组第一项用于收藏功能的展示图片[‘https://b.bdstatic.com/miniapp/images/demo-dog.png‘]

    toolbar-config 参数说明

    属性名类型必填默认值说明
    placeholderString输入框提示文字
    moduleListArray[‘comment’, ‘like’, ‘favor’, ‘share’]显示的互动模块,对应默认值分别是:评论数、点赞、收藏、分享
    shareObject若 moduleList 里配置了 share 模块,该参数为必填
    share.titleString分享标题
    share.contentString分享内容
    share.imageUrlString分享图标
    share.pathString页面 path ,必须是以 / 开头的完整路径,如果 path 中参数包含中文字符,需对中文字符进行编码

    view-more-style 参数说明

    属性名类型必填默认值说明
    colorString‘#3388ff’『全部 xx 条』的字体颜色,默认为视觉提供色号,开发者可传入自定义色号

    代码示例 1:列表组件放入页面

    页面中引用动态库组件的方式是:在页面的 json 配置的 usingSwanComponents 字段中声明组件引用。

    • JSON
     
     
     
    1. {
    2. "navigationBarTitleText": "评论列表",
    3. "usingSwanComponents": {
    4. "comment-list": "dynamicLib://myDynamicLib/comment-list"
    5. }
    6. }

    在页面中放入列表组件,传入必要的参数。

    • SWAN
    • JS
     
     
     
    1. comment-param="{{commentParam}}"
    2. detail-path="{{detailPath}}"
    3. toolbar-config="{{toolbarConfig}}"
    4. bindclickcomment="clickComment">
     
     
     
    1. Page({
    2. data: {
    3. commentParam: {},
    4. detailPath: '/pages/detail/index?params1=abd',
    5. // 底部互动 bar 的配置
    6. toolbarConfig: {
    7. // 若 moduleList 中配置有 share 模块,默认是有,则该属性为必填,title 必传
    8. share: {
    9. title: '测试文章标题'
    10. }
    11. }
    12. },
    13. onLoad(query) {
    14. this.setData({
    15. commentParam: {
    16. snid: '10070000311753961',
    17. path: '/pages/comment/index?snid=test_snid57',
    18. title: '测试文章标题',
    19. content: '测试文章内容'
    20. }
    21. });
    22. },
    23. onReady() {
    24. requireDynamicLib('myDynamicLib').listenEvent();
    25. },
    26. clickComment(e) {
    27. }
    28. });

    代码示例 2:列表支持折叠

    • SWAN
    • JS
    • JSON
    • CSS
     
     
     
    1. {{header.title}}
    2. {{header.author}}
    3. {{header.time}}
    4. {{item.data}}
    5. class="content-img"
    6. src="{{item.data.src}}"
    7. original-src="{{item.data.src}}"
    8. mode="widthFix"
    9. preview="true"
    10. lazy-load="true"/>
    11. comment-param="{{commentParam}}"
    12. is-folded="{{true}}"
    13. fold-num="{{foldNum}}"
    14. toolbar-config="{{toolbarConfig}}"
    15. bindclickcomment="clickComment"
    16. bindviewmore="viewMore">
    17. 欢迎使用智能小程序动态库
    18. 欢迎使用智能小程序动态库
    19. 欢迎使用智能小程序动态库
    20. class="img">
    21. 欢迎使用智能小程序动态库
    22. 欢迎使用智能小程序动态库
    23. 欢迎使用智能小程序动态库
     
     
     
    1. Page({
    2. data: {
    3. commentParam: {},
    4. header: {
    5. title: '心疼!中国自行车女将卷入摔车事故 腹部扎入3厘米木刺坚持完赛',
    6. avatar: 'https://b.bdstatic.com/miniapp/images/demo-dog.png',
    7. author: '百度智能小程序',
    8. time: '2020年04月14日'
    9. },
    10. content: {
    11. items: [
    12. {
    13. type: 'image',
    14. data: {
    15. src: 'https://b.bdstatic.com/miniapp/images/demo-dog.png'
    16. }
    17. },
    18. {
    19. type: 'text',
    20. data: '测试文字'
    21. }
    22. ]
    23. },
    24. // 折叠展示最大评论条数
    25. foldNum: 5,
    26. // 底部互动 bar 的配置
    27. toolbarConfig: {
    28. // 若 moduleList 中配置有 share 模块,默认是有,则该属性为必填,title 必传
    29. share: {
    30. title: '心疼!中国自行车女将卷入摔车事故 腹部扎入3厘米木刺坚持完赛'
    31. }
    32. }
    33. },
    34. onLoad(query) {
    35. this.setData({
    36. commentParam: {
    37. snid: '10070000311753961',
    38. path: '/pages/comment/index?snid=test_snid57',
    39. title: '测试文章标题',
    40. content: '测试文章内容'
    41. }
    42. });
    43. },
    44. onReady() {
    45. requireDynamicLib('myDynamicLib').listenEvent();
    46. },
    47. clickComment(e) {
    48. swan.showToast({
    49. title: 'click comment success'
    50. });
    51. },
    52. viewMore() {
    53. // swan.showToast({
    54. // title: 'click viewmore success'
    55. // });
    56. }
    57. });
     
     
     
    1. {
    2. "navigationBarTitleText": "折叠列表页",
    3. "usingSwanComponents": {
    4. "comment-list": "dynamicLib://myDynamicLib/comment-list"
    5. }
    6. }
     
     
     
    1. .article-header {
    2. padding: 0 39.8rpx;
    3. }
    4. .article-header .title {
    5. display: block;
    6. font-size: 56rpx;
    7. line-height: 1.5;
    8. font-weight: 700;
    9. }
    10. .article-header .source {
    11. margin-top: 56rpx;
    12. display: flex;
    13. align-items: flex-start;
    14. }
    15. .article-header .source image {
    16. width: 82rpx;
    17. height: 82rpx;
    18. border-radius: 100%;
    19. margin-right: 18.7rpx;
    20. background-color: #eef1f4;
    21. background-size: 37.4rpx 37.4rpx;
    22. background-repeat: no-repeat;
    23. background-position: center center;
    24. background-image: url(../common/assets/logo-default.png);
    25. }
    26. .article-header .info {
    27. display: flex;
    28. flex-direction: column;
    29. justify-content: center;
    30. height: 82rpx;
    31. }
    32. .article-header .info .author {
    33. font-size: 37.4rpx;
    34. line-height: 1;
    35. display: block;
    36. color: #000;
    37. margin-bottom: 16.4rpx;
    38. }
    39. .article-header .info .time {
    40. display: block;
    41. color: #999;
    42. font-size: 28rpx;
    43. line-height: 1;
    44. }
    45. .article-content {
    46. color: #000;
    47. font-size: 44.5rpx;
    48. line-height: 1.58;
    49. letter-spacing: 2.84;
    50. margin-bottom: 70.2rpx;
    51. }
    52. .article-content .content-img {
    53. width: 100%;
    54. margin-top: 70.2rpx;
    55. vertical-align: bottom;
    56. background-color: #eef1f4;
    57. background-size: 74.9rpx 74.9rpx;
    58. background-repeat: no-repeat;
    59. background-position: center center;
    60. background-image: url(../common/assets/logo-default.png);
    61. }
    62. .article-content .content-p {
    63. margin: 57.3rpx 39.8rpx -12.9rpx 39.8rpx;
    64. text-align: justify;
    65. word-break: break-all;
    66. }
    67. .list-after {
    68. padding: 30rpx 18rpx 90rpx;
    69. }
    70. .comment-list-folded-bottom-margin {
    71. height: 14.4rpx;
    72. background-color: #f5f5f5;
    73. }

    调起评论发布功能

    若开发者希望调起评论发布器,且与组件内的评论发布逻辑保持一致(发布成功插入列表第一条,且滚动到列表顶部),可使用如下方法:
    在 js 文件中,将 add-comment 属性设为 true ,即可调起评论发布器。

    前提是评论列表组件已渲染。

    代码示例:列表组件放入浮层且自定义底部 toolbar

    • SWAN
    • JS
    • JSON
    • CSS
     
     
     
    1. class="img">
    2. 这是背景
    3. 全部评论
    4. class="float-list-component"
    5. add-comment="{{addComment}}"
    6. is-page-scroll="{{false}}"
    7. comment-param="{{commentParam}}"
    8. need-toolbar="{{false}}"
    9. bindclickcomment="clickComment">
    10. 关闭
    11. 评论详情
    12. class="float-detail-component"
    13. add-comment="{{publishComment}}"
    14. srid="{{srid}}"
    15. is-page-scroll="{{false}}"
    16. comment-param="{{commentParam}}"
    17. need-toolbar="{{false}}"
    18. back-list-after-delete="{{false}}"
    19. binddelete="detailDelete">
     
     
     
    1. Page({
    2. data: {
    3. commentParam: {},
    4. addComment: {},
    5. showList: false,
    6. showDetail: false,
    7. srid: ''
    8. },
    9. onLoad() {
    10. this.setData({
    11. commentParam: {
    12. snid: '10070000311753961',
    13. path: '/pages/comment/index',
    14. title: '测试文章标题',
    15. 'snid_type': ''
    16. }
    17. });
    18. },
    19. clickComment(e) {
    20. this.setData({
    21. srid: e.data.srid,
    22. showDetail: true,
    23. showList: false
    24. });
    25. },
    26. addComment() {
    27. const showDetail = this.data.showDetail;
    28. if (!showDetail) {
    29. this.setData({
    30. showList: true,
    31. addComment: true
    32. }, () => {
    33. // 需要设为 false 的原因:因为调起发布监听 addComment 的变化,如果一直为 true,无法再次调起
    34. this.setData({
    35. 'addComment': false
    36. });
    37. });
    38. }
    39. else {
    40. this.setData({
    41. showList: false,
    42. publishComment: true
    43. }, () => {
    44. // 需要设为 false 的原因:因为调起发布监听 addComment 的变化,如果一直为 true,无法再次调起
    45. this.setData({
    46. 'publishComment': false
    47. });
    48. });
    49. }
    50. },
    51. /**
    52. * 详情删除后的回调
    53. *
    54. * @param {Object} options 返回的相关数据,{status, data}
    55. * @property {string} srid 评论 id
    56. */
    57. detailDelete(options) {
    58. if (options.data.srid) {
    59. this.setData({
    60. showDetail: false,
    61. showList: true
    62. });
    63. }
    64. },
    65. closeDetail() {
    66. this.setData({
    67. showDetail: false,
    68. showList: true
    69. });
    70. }
    71. });
     
     
     
    1. {
    2. "navigationBarTitleText": "智能小程序示例",
    3. "usingSwanComponents": {
    4. "comment-list": "dynamicLib://myDynamicLib/comment-list",
    5. "comment-detail": "dynamicLib://myDynamicLib/comment-detail"
    6. }
    7. }
     
     
     
    1. page {
    2. height: 100%;
    3. }
    4. .container {
    5. display: flex;
    6. flex-direction: column;
    7. min-height: 100%;
    8. }
    9. .img {
    10. width: 100%;
    11. position: relative;
    12. z-index: -1;
    13. -webkit-overflow: visible;
    14. }
    15. .bg {
    16. flex: 1;
    17. }
    18. .float-list-wrap,
    19. .float-detail-wrap {
    20. background-color: #fff;
    21. position: fixed;
    22. bottom: 0;
    23. left: 0;
    24. display: block;
    25. height: 900rpx;
    26. animation: climbup .5s;
    27. width: 100%;
    28. z-index: 99;
    29. border-top: 1px solid #666;
    30. border-radius: 10rpx;
    31. }
    32. .float-title {
    33. text-align: center;
    34. padding: 20rpx 0;
    35. }
    36. .float-list-component,
    37. .float-detail-component {
    38. height: 100%;
    39. }
    40. .float-list,
    41. .float-detail {
    42. overflow-y: scroll;
    43. height: 700rpx;
    44. /* my-toolbar 有多高,这里就设多少 */
    45. margin-bottom: 90rpx;
    46. }
    47. .float-detail-close {
    48. float: right;
    49. padding: 20rpx;
    50. }
    51. .my-toolbar {
    52. position: fixed;
    53. bottom: 0;
    54. width: 100%;
    55. height: 90rpx;
    56. background-color: #fff;
    57. z-index: 999;
    58. font-size: 28.99rpx;
    59. }
    60. @keyframes climbup {
    61. /* 因为浮层块高度为800rpx */
    62. 0% {
    63. height: 0;
    64. }
    65. 25% {
    66. height: 200rpx;
    67. }
    68. 50% {
    69. height: 400rpx;
    70. }
    71. 75% {
    72. height: 600rpx;
    73. }
    74. 100% {
    75. height: 900rpx;
    76. }
    77. }

    Bug & Tip

    • Tip:评论列表数据开发者无法单独获取,也无需配置,评论列表会托管给组件,开发者接入组件之后,用户评论发布后会展现在评论列表上(自己可见),审核通过后会全体用户可见。
    • Tip:openid 和百度 App 登录状态(合称登录状态)会影响用户的发布评论、举报、删除、消息提醒、跳转个人主页和页面收藏(合称互动行为),未登录用户仅可以浏览评论和点赞。
    • Tip:收藏功能在基础库 3.190.1 以上可用。
    • Tip:由于 swan.login API 的非兼容改造,一站式互动组件统一改为在组件内强登录。

    文章标题:创新互联百度小程序教程:comment-list 评论列表组件
    分享URL:http://www.csdahua.cn/qtweb/news9/469059.html

    网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网