使用for of来判断数据:@Component export struct MyTextInput { @State private inputText: string = "" @Prop @Watch('onTextUpdated') text:string = "" public maxLength: number = 10 onTextUpdated() { this.inputText = this.sliceTools(this.text, this.maxLength) } aboutToAppear(): void { this.onTextUpdated() } sliceTools(str: string, maxLength: number) { let res = ""; for (const element of str) { if (res.length + element.length > maxLength) { return res } else { res = res + element } } return res; } build() { TextInput({ text: $$this.inputText }) .maxLength(this.maxLength) } }
使用for of来判断数据: