鴻蒙開發(fā)-Observed及ObjectLink使用

@Entry
@Component
struct LinkCase {
  @State message: string = 'Hello World'
  @State foodList: Food[] = [
    new Food({
      id: 1,
      name: '魚香肉絲',
      price: 18.2,
      num: 10
    }),
  new Food({
      id: 2,
      name: '醋溜丸子',
      price: 12.2,
      num: 39
    }),
    new Food({
      id: 3,
      name: '雜糧煎餅',
      price: 12.2,
      num: 12
    }),
  ]

  build() {
    Row() {
      Column() {
        ForEach(this.foodList, (item: Food, index: number) => {
          FoodItem({item: item})
        })
        ButtonBottom({myFoodList: $foodList})
      }
      .width('100%')
    }
    .height('100%')
  }
}


@Component
struct FoodItem {
  @ObjectLink
  item: Food
  build() {
    Row() {
      Text(this.item.name).textStyle()
      Text(this.item.price.toString()).textStyle()
      Row() {
        Text('-').actionStyle()
          .onClick(() => {
            this.item.num--
          })
          .visibility(this.item.num <= 0 ? Visibility.Hidden: Visibility.Visible)
        Text(this.item.num.toString())
          .textStyle()
          .visibility(this.item.num <= 0 ? Visibility.Hidden: Visibility.Visible)
        Text('+').actionStyle()
          .onClick(() => {
            this.item.num++
          })
      }
        .layoutWeight(1)

    }
    .width('100%')
    .padding({
      top: 10,
      bottom: 10
    })
  }
}

@Extend(Text)
function actionStyle() {
  .width(40)
  .height(40)
  .backgroundColor(Color.Gray)
  .borderRadius(20)
  .textAlign(TextAlign.Center)
  .fontColor(Color.White)
  .fontSize(20)
}
@Extend(Text)
function textStyle() {
  .layoutWeight(1).textAlign(TextAlign.Center).fontSize(20)
}

@Component
struct ButtonBottom {
  @Link
  myFoodList: Food[]
  build() {
    Button('更改數(shù)量')
      .onClick(() => {
        // this.myFoodList.forEach((item: Food) => {
        //   item.num++
        // })
        this.myFoodList = this.myFoodList.map((item: Food) => {
          item.num++
          return item
        })
      })
  }
}
interface IFood {
  id: number
  name: string
  price: number
  num: number
}

@Observed
class Food implements IFood{
  id: number
  name: string
  price: number
  num: number

  constructor(food: IFood) {
    this.id = food.id
    this.name = food.name
    this.price = food.price
    this.num = food.num
  }
}
image.png
@Entry
@Component
struct ZhihuComment {
  @State message: string = 'Hello World'
  @State replyList:  ReplyItem[] = [
    new ReplyItem({
      id: 0,
      icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
      name: '張三' + 0,
      content: '評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容' + 0,
      publicTime: '10-01',
      area: '北京',
      likeNum: 100 + 0
    }),
    new ReplyItem({
      id: 1,
      icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
      name: '張三' + 1,
      content: '評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容' + 1,
      publicTime: '10-01',
      area: '北京',
      likeNum: 100 + 1
    }),
    new ReplyItem({
      id: 2,
      icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
      name: '張三' + 2,
      content: '評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容' + 2,
      publicTime: '10-01',
      area: '北京',
      likeNum: 100 + 2
    })
    ,
    new ReplyItem({
      id: 2,
      icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
      name: '張三' + 2,
      content: '評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容' + 2,
      publicTime: '10-01',
      area: '北京',
      likeNum: 100 + 2
    })
    ,
    new ReplyItem({
      id: 2,
      icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
      name: '張三' + 2,
      content: '評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容' + 2,
      publicTime: '10-01',
      area: '北京',
      likeNum: 100 + 2
    })
    ,
    new ReplyItem({
      id: 2,
      icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
      name: '張三' + 2,
      content: '評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容' + 2,
      publicTime: '10-01',
      area: '北京',
      likeNum: 100 + 2
    })
    ,
    new ReplyItem({
      id: 2,
      icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
      name: '張三' + 2,
      content: '評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容' + 2,
      publicTime: '10-01',
      area: '北京',
      likeNum: 100 + 2
    })
    ,
    new ReplyItem({
      id: 2,
      icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
      name: '張三' + 2,
      content: '評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容' + 2,
      publicTime: '10-01',
      area: '北京',
      likeNum: 100 + 2
    })
  ]

  changeLike(item: ReplyItem) {
    if (item.likeFlag) {
      item.likeFlag = false
      item.likeNum = item.likeNum - 1
    } else {
      item.likeFlag = true
      item.likeNum = item.likeNum + 1
    }

    const index = this.replyList.findIndex((obj) => obj.id == item.id)
    AlertDialog.show({message: 'test' + index + " " + item.likeNum})
    this.replyList[index] = {...item}
  }
  aboutToAppear() {
    // for (let i = 0; i < 10; i++) {
    //   this.replyList.push({
    //     id: i,
    //     icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
    //     name: '張三' + i,
    //     content: '評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容評論內(nèi)容' + i,
    //     publicTime: '10-01',
    //     area: '北京',
    //     likeNum: 100 + i
    //   })
    // }
  }
  build() {
    Stack({alignContent: Alignment.Bottom}) {
      Scroll() {
        Column() {
          ZhiHuHeader()
          // ZhihuItem({item: {
          //   id: 1,
          //   icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
          //   name: '張三',
          //   content: '內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容',
          //   publicTime: '10-10',
          //   area: '上海',
          //   likeNum: 100
          // }})
          Divider()
            .width('100%')
            .color('#f4f4f4')
            .strokeWidth(8)
            .margin({
              top: 10
            })
          Row(){
            Text('回復(fù)')
              .textAlign(TextAlign.Start)
              .fontWeight(400)
              .fontSize(16)
              .fontColor('#333333')
          }
          .width('100%')
          .margin({
            left: 15,
            top: 15
          })
          ForEach(this.replyList, (item: ReplyItem, index: number) => {
            ZhihuItem({item: item, changeLike: (obj: ReplyItem) => {
              this.changeLike(obj)
            }})
          })

        }

      }.padding({
        bottom: 60
      })
      .align(Alignment.Top)
      .alignSelf(ItemAlign.Start)
      ReplyAdd({addReply: (item: ReplyItem) => {
        this.replyList.unshift(item)
      }})
    }
    .height('100%')

  }
}

@Component
struct ReplyAdd {
  @State content: string = ""
  addReply: (item: ReplyItem) => void = () => {}
  build() {
    Row() {
      TextInput({
        placeholder: '回復(fù)~',
        text: this.content
      }).layoutWeight(1)
        .onChange((value) => {
          this.content = value
        })
      Text('發(fā)布')
        .margin({
          left: 10
        }).fontColor(Color.Blue)
        .onClick(() => {
          const  replyItem: ReplyItem = {
            id: Date.now(),
            icon: 'https://img1.baidu.com/it/u=2198066039,3336806063&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
            name: '李四',
            area: '北京',
            content: this.content,
            publicTime: `${new  Date().getMonth() + 1}-${new Date().getDate()}`,
            likeNum: 0,
            likeFlag: false
          }
          this.addReply(replyItem);
        })
    }
    .padding({
      left: 10,
      right: 10
    })
    .width('100%')
    .height(60)
    .backgroundColor(Color.White)
    .border({
      color: '#f4f5f6',
      width: {
        top: 1
      }
    })
  }
}

@Component
struct ZhiHuHeader {
  build() {
    Row(){
      Row() {
        Image($r('app.media.back'))
          .width(12)
          .height(12)
          .fillColor('#848484')
      }
      .width(24)
      .aspectRatio(1)
      .backgroundColor('#f4f4f4')
      .borderRadius(20)
      .justifyContent(FlexAlign.Center)
      .alignItems(VerticalAlign.Center)
      .margin({
        left:15
      })

      Text('評論回復(fù)')
        .layoutWeight(1)
        .textAlign(TextAlign.Center)
        .fontSize(18)
        .padding({
          right: 39
        })
        .fontWeight(400)
    }
    .width('100%')
    .alignItems(VerticalAlign.Center)
    .border({
      color: '#f4f4f4',
      width: {
        bottom:1
      }
    })
    .height(50)

  }
}

@Component
struct ZhihuItem {
  @ObjectLink
  item: ReplyItem
  changeLike: (item: ReplyItem) => void = () => {}
  build() {
    Row() {
      Image(this.item.icon)
        .width(50)
        .height(50)
        .borderRadius(25)

      Column() {
        Text(this.item.name)
          .fontSize(20)
          .fontColor('#333333')
          .fontWeight(400)

        Text(this.item.content)
          .fontSize(14)
          .fontColor('#666666')
          .margin({
            top: 10
          })
          .lineHeight(18)
          .maxLines(2)
          .textOverflow({
            overflow: TextOverflow.Ellipsis
          })

        Row() {
          Row() {
            Text(this.item.publicTime)
              .fontSize(12)
              .fontColor('#999999')
            Text(`IP屬地${this.item.area}`)
              .fontSize(12)
              .fontColor('#999999')
              .margin({
                left: 5
              })
          }
          .layoutWeight(1)
          Row() {
            Image($r('app.media.like'))
              .width(20)
              .height(20)
            Text(this.item.likeNum.toString())
              .fontColor('#999999')
              .fontSize(12)
              .margin({
                left: 5
              })
          }.onClick(() => {
            // this.changeLike(this.item as ReplyItem);
            if (this.item.likeFlag) {
              this.item.likeFlag = false
              this.item.likeNum = this.item.likeNum - 1
            } else {
              this.item.likeFlag = true
              this.item.likeNum = this.item.likeNum + 1
            }
          })
        }
          .width('100%')
        .margin({
          top: 20
        })
      }
      .alignItems(HorizontalAlign.Start)
      .layoutWeight(1)

      .margin({
        left: 15
      })
    }
    .alignItems(VerticalAlign.Top)
    .width('100%')
    .padding({
      left: 15,
      right: 15,
      top: 15
    })
  }
}

class ReplyClass {
  id: number
  icon: string
  name: string
  content: string
  publicTime: string
  area: string
  likeNum: number
  likeFlag?:boolean = false
}
@Observed
class ReplyItem extends ReplyClass{
  constructor(replyClass: ReplyClass) {
    super()
    this.id = replyClass.id
    this.icon = replyClass.icon
    this.name = replyClass.name
    this.content = replyClass.content
    this.publicTime = replyClass.publicTime
    this.area = replyClass.area
    this.likeNum = replyClass.likeNum
    this.likeFlag = replyClass.likeFlag
  }
}
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市隘蝎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖吠式,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柱宦,死亡現(xiàn)場離奇詭異唉匾,居然都是意外死亡孕讳,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進(jìn)店門巍膘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人芋簿,你說我怎么就攤上這事峡懈。” “怎么了与斤?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵肪康,是天一觀的道長。 經(jīng)常有香客問我撩穿,道長磷支,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任食寡,我火速辦了婚禮雾狈,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘抵皱。我一直安慰自己善榛,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布呻畸。 她就那樣靜靜地躺著移盆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伤为。 梳的紋絲不亂的頭發(fā)上咒循,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天,我揣著相機與錄音绞愚,去河邊找鬼叙甸。 笑死,一個胖子當(dāng)著我的面吹牛爽醋,可吹牛的內(nèi)容都是我干的蚁署。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蚂四,長吁一口氣:“原來是場噩夢啊……” “哼光戈!你這毒婦竟也來了哪痰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤久妆,失蹤者是張志新(化名)和其女友劉穎晌杰,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體筷弦,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡肋演,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了烂琴。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片爹殊。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖奸绷,靈堂內(nèi)的尸體忽然破棺而出梗夸,到底是詐尸還是另有隱情,我是刑警寧澤号醉,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布反症,位于F島的核電站,受9級特大地震影響畔派,放射性物質(zhì)發(fā)生泄漏铅碍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一线椰、第九天 我趴在偏房一處隱蔽的房頂上張望胞谈。 院中可真熱鬧,春花似錦士嚎、人聲如沸呜魄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽爵嗅。三九已至,卻和暖如春笨蚁,著一層夾襖步出監(jiān)牢的瞬間睹晒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工括细, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留伪很,地道東北人。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓奋单,卻偏偏與公主長得像锉试,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子览濒,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容