零基础开始学习鸿蒙开发-评论区的制作

前言:利用TextArea文本域制作一个评论区,评论区下面显示评论区的内容

1.下面简单介绍TextArea的使用。

代码示例:

//在placeholder设置提示用户输入的内容,通过onchange事件监听文本域输入的内容/
 TextArea({ placeholder: '请输入评论内容' }).onChange((content: string) => {
        this.content = content;
      }).height(200).width(270)

2.通过List展示文本评论区的内容

代码示例:
//通过ForEach循环遍历初始化的数据,或者请求返回来的数据
 List() {
          //通过foEach循环获取数据
          //循环遍历我们的游戏。
          ForEach(this.commentList, (item) => {
            ListItemGroup() {
              ListItem() {
                Row({ space: 2 }) {
                  Text('时间:')
                  Text(item.createTime)
                }
              }
              ListItem() {
                Row({ space: 2 }) {
                  Text('评论内容:')
                  Text(item.content)
                }
              }
            }
          }, item => item)
        }

3.下面是完整的评论区的制作代码

import  postCommont from  '../utils/CommentUtil'
import ArrayList from '@ohos.util.ArrayList';
@Observed
export  class CommentEntity{
  private id:number;
  //定义内容
  private content:string;
  //定义时间
  private createTime:string;
}
@Entry
@Component
struct textAreaPage {
  //定义一个空的数组
  @State commentList: Array<CommentEntity> = [].concat(
    {
      "createTime": "2023-12-2 15:00:00",
      "content": "外你"
    }
  );
  @State  private content: string=""
  @State private commentURl: string = 'http://ntek44b.nat.ipyingshe.com/comment';
 /* @State private getList: string = 'http://ntek44b.nat.ipyingshe.com/getList';*/
  //定义一个空字符串数组
  build() {
    Column({ space: 8 }) {
      TextArea({ placeholder: '请输入评论内容' }).onChange((content: string) => {
        this.content = content;
      }).height(200).width(270)
      Button('评论留言').onClick(async () => {
        //将请求写入数据
        let res = await postCommont(this.commentURl, this.content);
        console.log("responseCode====",res.responseCode)
        console.log("res====",JSON.stringify(res))
      }).width("80").height("30")
      Text('-----------以下显示评论内容-------').width("100%")
      Column({ space: 2 }) {
        //下面用于一个List展示评论的内容
       List() {
          //通过foEach循环获取数据
          //循环遍历我们的游戏。
          ForEach(this.commentList, (item) => {
            ListItemGroup() {
              ListItem() {
                Row({ space: 2 }) {
                  Text('时间:')
                  Text(item.createTime)
                }
              }
              ListItem() {
                Row({ space: 2 }) {
                  Text('评论内容:')
                  Text(item.content)
                }
              }
            }
          }, item => item)
        }
      }
    }.width("100%").height("100").backgroundColor(Color.White)
  }
}

4.效果如下图

  • 9
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心之所想,行则将至

创作不易,希望大家多多鼓励支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值