HarmonyOS @Prop参数传递问题

import { buffer } from '@kit.ArkTS';
import { utilsBufferDataToJson } from '../../../common/Utils/Utils';

@Component
export struct DHTestPageCell {
  @Prop buff: ArrayBuffer;

  build() {
    Text(JSON.stringify(utilsBufferDataToJson(this.buff)) + ': 1234')
  }
}

@Entry
@Component
struct DHTestPage {
  @State data: ArrayBuffer[] = []

  aboutToAppear(): void {
    this.getData()
  }

  getData() {
    let names = ['xx', 'xx', 'xx', 'xx', "xx"]
    names.forEach(element => {
      let buff = this.stringToUint8Array(element)
      this.data.push(buff);
    });
    console.log("buff", this.data)
  }

  stringToUint8Array(str: string) {
    return buffer.from(str, 'utf-8').buffer
    // return new Uint8Array(buffer.from(str,'utf-8').buffer);
  }

  build() {
    Row() {
      List() {
        ForEach(this.data, (buff: ArrayBuffer, index: number) => {
          ListItem() {
            DHTestPageCell({ buff: buff })
          }
        })
      }
    }
    .height('100%')
  }
}
export function utilsBufferDataToJson(customData: ArrayBuffer): object | undefined {
  let decoder = util.TextDecoder.create("utf-8", { ignoreBOM: true })
  let jsonString = decoder.decodeWithStream(new Uint8Array(customData), { stream: false });
  jsonString = jsonString ?? ''
  if (jsonString.length <= 0) {
    return undefined
  }
  return JSON.parse(jsonString ?? '');
}

list的buff参数传递到DHTestPageCell中为什么是空?

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

参考示例:

class Book {
  public title: string;
  public pages: number;
  public readIt: boolean = false;

  constructor(title: string, pages: number) {
    this.title = title;
    this.pages = pages;
  }
}

@Component
struct ReaderComp {
  @Prop book: Book = new Book("", 0);
  @Prop buff: Array<string> = [];

  build() {
    Row() {
      Text(this.book.title)
      Text(`...has${this.book.pages} pages!`)
      Text(`...${this.book.readIt ? "I have read" : 'I have not read it'}`)//.onClick(() => this.book.readIt = true)

        .onClick(() => {
          console.log("buff", this.buff);
        })
    }
  }
}

@Entry
@Component
struct Library {
  @State book: Book = new Book('100 secrets of C++', 765);
  @State proList: Array<string> = ['xx', 'xx', 'xx', 'xx', "xx"];

  aboutToAppear(): void {
    console.log("buff", this.proList)
  }

  build() {
    Column() {
      Button('hhhh')
      //ReaderComp({ book: this.book })
      ReaderComp({ buff: this.proList, book: this.book })
      //ReaderComp({ book: this.book })
    }
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
router传递hashmap参数问题
1594浏览 • 1回复 待解决
HarmonyOS 开发@Prop问题
390浏览 • 1回复 待解决
关于JS http请求参数传递问题
7485浏览 • 2回复 待解决
HarmonyOS rest参数如何传递
58浏览 • 1回复 待解决
HarmonyOS 多个@Prop变化顺序问题
325浏览 • 1回复 待解决
HarmonyOS web组件参数传递报错
42浏览 • 1回复 待解决
HarmonyOS http post请求参数传递
91浏览 • 1回复 待解决
HarmonyOS 怎么把组件作为参数传递
42浏览 • 1回复 待解决
HarmonyOS router参数不能传递函数
44浏览 • 1回复 待解决
windowClass.setUIContent是否支持传递参数
2137浏览 • 1回复 待解决
ETS API求助 Navigator如何传递参数
7366浏览 • 1回复 待解决