1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16// Customize the status data class. 17class Model { 18 value: string = ''; 19 constructor(value: string) { 20 this.value = value; 21 } 22} 23 24@Entry 25@Component 26struct EntryComponent { 27 build() { 28 Column(){ 29 MyComponentState() //MyComponent1 in this document 30 MyComponentState() //MyComponent2 in this document 31 } 32 } 33} 34 35@Component 36struct MyComponentState { 37 38 @State title: Model = new Model('Hello World') 39 @State count: number = 0 40 private toggle: boolean = true 41 42 build() { 43 44 Column(){ 45 Text(`${this.title.value}`) 46 Button() 47 .onClick(() => { 48 this.toggle != this.toggle; 49 this.title.value = this.toggle ? 'Hello World' : 'Hello Ace'; 50 }) // Modify the internal status data of MyComponent using the anonymous method. 51 52 Button() { 53 Text(`click times: ${this.count}`) 54 .fontSize(10) 55 }.onClick(() => { 56 this.count += 1 57 }) 58 } 59 60 } 61} 62