diff --git a/zh-cn/application-dev/quick-start/arkts-animatable-extend.md b/zh-cn/application-dev/quick-start/arkts-animatable-extend.md index 37976872e3ee57c5a5c66586a1194dca814f3f95..b161981521d4bceee24402d47de5a837372d6966 100644 --- a/zh-cn/application-dev/quick-start/arkts-animatable-extend.md +++ b/zh-cn/application-dev/quick-start/arkts-animatable-extend.md @@ -101,7 +101,7 @@ class PointVector extends Array implements AnimatableArithmetic)[i].plus((rhs as Array)[i])) } return result } @@ -109,14 +109,14 @@ class PointVector extends Array implements AnimatableArithmetic)[i].subtract((rhs as Array)[i])) } return result } multiply(scale: number): PointVector { let result = new PointVector([]) for (let i = 0; i < this.length; i++) { - result.push(this[i].multiply(scale)) + result.push((this as Array)[i].multiply(scale)) } return result } @@ -125,14 +125,14 @@ class PointVector extends Array implements AnimatableArithmetic)[i].equals((rhs as Array)[i])) { return false } } return true } - get():Array<[x: number, y: number]> { - let result = [] + get(): Array { + let result: Array = [] this.forEach(p => result.push([p.x, p.y])) return result } diff --git a/zh-cn/application-dev/quick-start/arkts-appstorage.md b/zh-cn/application-dev/quick-start/arkts-appstorage.md index a384791cffc1156c0397ee66d1c44155c5b5c810..dd340c601a50e1f04fddca8147fca635a6f1a207 100644 --- a/zh-cn/application-dev/quick-start/arkts-appstorage.md +++ b/zh-cn/application-dev/quick-start/arkts-appstorage.md @@ -144,21 +144,22 @@ AppStorage是单例,它的所有API都是静态的,使用方法类似于中L ```ts AppStorage.SetOrCreate('PropA', 47); -let storage: LocalStorage = new LocalStorage({ 'PropA': 17 }); -let propA: number = AppStorage.Get('PropA') // propA in AppStorage == 47, propA in LocalStorage == 17 -var link1: SubscribedAbstractProperty = AppStorage.Link('PropA'); // link1.get() == 47 -var link2: SubscribedAbstractProperty = AppStorage.Link('PropA'); // link2.get() == 47 -var prop: SubscribedAbstractProperty = AppStorage.Prop('PropA'); // prop.get() = 47 +let storage: LocalStorage = new LocalStorage(); +storage['PropA'] = 17; +let propA: number | undefined = AppStorage.Get('PropA') // propA in AppStorage == 47, propA in LocalStorage == 17 +let link1: SubscribedAbstractProperty = AppStorage.Link('PropA'); // link1.get() == 47 +let link2: SubscribedAbstractProperty = AppStorage.Link('PropA'); // link2.get() == 47 +let prop: SubscribedAbstractProperty = AppStorage.Prop('PropA'); // prop.get() = 47 link1.set(48); // two-way sync: link1.get() == link2.get() == prop.get() == 48 prop.set(1); // one-way sync: prop.get()=1; but link1.get() == link2.get() == 48 link1.set(49); // two-way sync: link1.get() == link2.get() == prop.get() == 49 -storage.get('PropA') // == 17 +storage.get('PropA') // == 17 storage.set('PropA', 101); -storage.get('PropA') // == 101 +storage.get('PropA') // == 101 -AppStorage.Get('PropA') // == 49 +AppStorage.Get('PropA') // == 49 link1.get() // == 49 link2.get() // == 49 prop.get() // == 49 @@ -172,7 +173,8 @@ prop.get() // == 49 ```ts AppStorage.SetOrCreate('PropA', 47); -let storage = new LocalStorage({ 'PropA': 48 }); +let storage = new LocalStorage(); +storage['PropA'] = 48; @Entry(storage) @Component @@ -242,8 +244,13 @@ struct Gallery2 { export struct TapImage { @StorageLink('tapIndex') @Watch('onTapIndexChange') tapIndex: number = -1; @State tapColor: Color = Color.Black; - private index: number; - private uri: Resource; + private index: number = 0; + private uri: Resource = { + id: 0, + type: 0, + moduleName: "", + bundleName: "" + }; // 判断是否被选中 onTapIndexChange() { @@ -313,9 +320,9 @@ struct Gallery2 { if (this.preIndex === item.id) { return } - var innerEvent = { eventId: item.id } + let innerEvent: emitter.InnerEvent = { eventId: item.id } // 选中态:黑变红 - var eventData = { + let eventData: emitter.EventData = { data: { "colorTag": 1 } @@ -324,9 +331,9 @@ struct Gallery2 { if (this.preIndex != -1) { console.info(`preIndex: ${this.preIndex}, index: ${item.id}, black`) - var innerEvent = { eventId: this.preIndex } + let innerEvent: emitter.InnerEvent = { eventId: this.preIndex } // 取消选中态:红变黑 - var eventData = { + let eventData: emitter.EventData = { data: { "colorTag": 0 } @@ -335,7 +342,6 @@ struct Gallery2 { } this.preIndex = item.id }) - }, (item: ViewData) => JSON.stringify(item)) }.columnsTemplate('1fr 1fr') } @@ -346,17 +352,26 @@ struct Gallery2 { @Component export struct TapImage { @State tapColor: Color = Color.Black; - private index: number; - private uri: Resource; + private index: number = 0; + private uri: Resource = { + id: 0, + type: 0, + moduleName: "", + bundleName: "" + }; onTapIndexChange(colorTag: emitter.EventData) { - this.tapColor = colorTag.data.colorTag ? Color.Red : Color.Black + if (colorTag.data != null) { + this.tapColor = colorTag.data.colorTag ? Color.Red : Color.Black + } } aboutToAppear() { //定义事件ID - var innerEvent = { eventId: this.index } - emitter.on(innerEvent, this.onTapIndexChange.bind(this)) + let innerEvent: emitter.InnerEvent = { eventId: this.index } + emitter.on(innerEvent, data => { + this.onTapIndexChange(data) + }) } build() { @@ -414,8 +429,13 @@ struct Gallery2 { export struct TapImage { @StorageLink('tapIndex') tapIndex: number = -1; @State tapColor: Color = Color.Black; - private index: number; - private uri: Resource; + private index: number = 0; + private uri: Resource = { + id: 0, + type: 0, + moduleName: "", + bundleName: "" + }; build() { Column() { diff --git a/zh-cn/application-dev/quick-start/arkts-environment.md b/zh-cn/application-dev/quick-start/arkts-environment.md index feba33c01e6e9c58303e65153de87ead8e3f16fe..03feb07cceb1f9aae09e35e46559cac7187a7f9f 100644 --- a/zh-cn/application-dev/quick-start/arkts-environment.md +++ b/zh-cn/application-dev/quick-start/arkts-environment.md @@ -35,7 +35,7 @@ Environment是ArkUI框架在应用程序启动时创建的单例对象。它为A ```ts // 将设备languageCode存入AppStorage中 Environment.EnvProp('languageCode', 'en'); -let enable = AppStorage.Get('languageCode'); +let enable: undefined = AppStorage.Get('languageCode'); @Entry @Component diff --git a/zh-cn/application-dev/quick-start/arkts-link.md b/zh-cn/application-dev/quick-start/arkts-link.md index d97c35667c5b89353015ec3ee4ad5108c244463f..914323f56b8e1d14f941995b8e2e9f3cc239050d 100644 --- a/zh-cn/application-dev/quick-start/arkts-link.md +++ b/zh-cn/application-dev/quick-start/arkts-link.md @@ -227,10 +227,10 @@ struct Parent { Column() { Child({ items: $arr }) ForEach(this.arr, - item => { + (item: void) => { Text(`${item}`) }, - item => item.toString() + (item: ForEachInterface) => item.toString() ) } } diff --git a/zh-cn/application-dev/quick-start/arkts-localstorage.md b/zh-cn/application-dev/quick-start/arkts-localstorage.md index 145929e29891278b93cd73f7183cfb6cf8899a20..8efc7e455859e5200eb49753680ae1a69247054a 100644 --- a/zh-cn/application-dev/quick-start/arkts-localstorage.md +++ b/zh-cn/application-dev/quick-start/arkts-localstorage.md @@ -191,36 +191,37 @@ link1.set(49); // two-way sync: link1.get() == link2.get() == prop.get() == 49 - \@LocalStorageLink绑定LocalStorage对给定的属性,建立双向数据同步。 ```ts - // 创建新实例并使用给定对象初始化 - let storage = new LocalStorage({ 'PropA': 47 }); - - @Component - struct Child { - // @LocalStorageLink变量装饰器与LocalStorage中的'ProA'属性建立双向绑定 - @LocalStorageLink('PropA') storLink2: number = 1; - - build() { - Button(`Child from LocalStorage ${this.storLink2}`) - // 更改将同步至LocalStorage中的'ProA'以及Parent.storLink1 - .onClick(() => this.storLink2 += 1) - } +// 创建新实例并使用给定对象初始化 +let storage = new LocalStorage(); +storage['PropA'] = 47; + +@Component +struct Child { + // @LocalStorageLink变量装饰器与LocalStorage中的'ProA'属性建立双向绑定 + @LocalStorageLink('PropA') storLink2: number = 1; + + build() { + Button(`Child from LocalStorage ${this.storLink2}`) + // 更改将同步至LocalStorage中的'ProA'以及Parent.storLink1 + .onClick(() => this.storLink2 += 1) } - // 使LocalStorage可从@Component组件访问 - @Entry(storage) - @Component - struct CompA { - // @LocalStorageLink变量装饰器与LocalStorage中的'ProA'属性建立双向绑定 - @LocalStorageLink('PropA') storLink1: number = 1; - - build() { - Column({ space: 15 }) { - Button(`Parent from LocalStorage ${this.storLink1}`) // initial value from LocalStorage will be 47, because 'PropA' initialized already - .onClick(() => this.storLink1 += 1) - // @Component子组件自动获得对CompA LocalStorage实例的访问权限。 - Child() - } +} +// 使LocalStorage可从@Component组件访问 +@Entry(storage) +@Component +struct CompA { + // @LocalStorageLink变量装饰器与LocalStorage中的'ProA'属性建立双向绑定 + @LocalStorageLink('PropA') storLink1: number = 1; + + build() { + Column({ space: 15 }) { + Button(`Parent from LocalStorage ${this.storLink1}`) // initial value from LocalStorage will be 47, because 'PropA' initialized already + .onClick(() => this.storLink1 += 1) + // @Component子组件自动获得对CompA LocalStorage实例的访问权限。 + Child() } } +} ``` @@ -233,37 +234,39 @@ link1.set(49); // two-way sync: link1.get() == link2.get() == prop.get() == 49 - Child组件中,Text绑定的storProp2 依旧显示47。 ```ts - // 创建新实例并使用给定对象初始化 - let storage = new LocalStorage({ 'PropA': 47 }); - // 使LocalStorage可从@Component组件访问 - @Entry(storage) - @Component - struct CompA { - // @LocalStorageProp变量装饰器与LocalStorage中的'ProA'属性建立单向绑定 - @LocalStorageProp('PropA') storProp1: number = 1; - - build() { - Column({ space: 15 }) { - // 点击后从47开始加1,只改变当前组件显示的storProp1,不会同步到LocalStorage中 - Button(`Parent from LocalStorage ${this.storProp1}`) - .onClick(() => this.storProp1 += 1) - Child() - } +// 创建新实例并使用给定对象初始化 +let storage = new LocalStorage(); +storage['PropA'] = 47; + +// 使LocalStorage可从@Component组件访问 +@Entry(storage) +@Component +struct CompA { + // @LocalStorageProp变量装饰器与LocalStorage中的'ProA'属性建立单向绑定 + @LocalStorageProp('PropA') storProp1: number = 1; + + build() { + Column({ space: 15 }) { + // 点击后从47开始加1,只改变当前组件显示的storProp1,不会同步到LocalStorage中 + Button(`Parent from LocalStorage ${this.storProp1}`) + .onClick(() => this.storProp1 += 1) + Child() } } +} - @Component - struct Child { - // @LocalStorageProp变量装饰器与LocalStorage中的'ProA'属性建立单向绑定 - @LocalStorageProp('PropA') storProp2: number = 2; +@Component +struct Child { + // @LocalStorageProp变量装饰器与LocalStorage中的'ProA'属性建立单向绑定 + @LocalStorageProp('PropA') storProp2: number = 2; - build() { - Column({ space: 15 }) { - // 当CompA改变时,当前storProp2不会改变,显示47 - Text(`Parent from LocalStorage ${this.storProp2}`) - } + build() { + Column({ space: 15 }) { + // 当CompA改变时,当前storProp2不会改变,显示47 + Text(`Parent from LocalStorage ${this.storProp2}`) } } +} ``` @@ -274,9 +277,10 @@ link1.set(49); // two-way sync: link1.get() == link2.get() == prop.get() == 49 ```ts // 构造LocalStorage实例 -let storage = new LocalStorage({ 'PropA': 47 }); +let storage = new LocalStorage(); +storage['PropA'] = 47; // 调用link9+接口构造'PropA'的双向同步数据,linkToPropA 是全局变量 -let linkToPropA = storage.link('PropA'); +let linkToPropA = storage.link('PropA'); @Entry(storage) @Component @@ -288,7 +292,7 @@ struct CompA { build() { Column() { Text(`incr @LocalStorageLink variable`) - // 点击“incr @LocalStorageLink variable”,this.storLink加1,改变同步回storage,全局变量linkToPropA也会同步改变 + // 点击“incr @LocalStorageLink variable”,this.storLink加1,改变同步回storage,全局变量linkToPropA也会同步改变 .onClick(() => this.storLink += 1) @@ -388,11 +392,10 @@ import UIAbility from '@ohos.app.ability.UIAbility'; import window from '@ohos.window'; export default class EntryAbility extends UIAbility { - storage: LocalStorage = new LocalStorage({ - 'PropA': 47 - }); + storage: LocalStorage = new LocalStorage(); onWindowStageCreate(windowStage: window.WindowStage) { + this.storage['PropA'] = 47; windowStage.loadContent('pages/Index', this.storage); } } diff --git a/zh-cn/application-dev/quick-start/arkts-observed-and-objectlink.md b/zh-cn/application-dev/quick-start/arkts-observed-and-objectlink.md index d63149dcb8418d6d5bf6028dcbeb074f1b1ff1fa..538c27e9e0787af8ced563bef4f0076b6e856b1b 100644 --- a/zh-cn/application-dev/quick-start/arkts-observed-and-objectlink.md +++ b/zh-cn/application-dev/quick-start/arkts-observed-and-objectlink.md @@ -126,7 +126,7 @@ this.b.a.c = 5 ```ts @Observed class DateClass extends Date { - constructor(args: any) { + constructor(args: number | string) { super(args) } } @@ -343,10 +343,10 @@ struct ViewB { build() { Column() { ForEach(this.arrA, - (item) => { + (item: ClassA) => { ViewA({ label: `#${item.id}`, a: item }) }, - (item) => item.id.toString() + (item: ClassA) => item.id.toString() ) // 使用@State装饰的数组的数组项初始化@ObjectLink,其中数组项是被@Observed装饰的ClassA的实例 ViewA({ label: `ViewA this.arrA[first]`, a: this.arrA[0] }) @@ -419,11 +419,11 @@ struct ItemPage { .width(100).height(100) ForEach(this.itemArr, - item => { + (item: string | Resource) => { Text(item) .width(100).height(100) }, - item => item + (item: string) => item ) } } @@ -439,14 +439,14 @@ struct IndexPage { ItemPage({ itemArr: this.arr[0] }) ItemPage({ itemArr: this.arr[1] }) ItemPage({ itemArr: this.arr[2] }) - Divider() + ForEach(this.arr, - itemArr => { + (itemArr: StringArray) => { ItemPage({ itemArr: itemArr }) }, - itemArr => itemArr[0] + (itemArr: string) => itemArr[0] ) Divider() @@ -454,7 +454,7 @@ struct IndexPage { Button('update') .onClick(() => { console.error('Update all items in arr'); - if (this.arr[0][0] !== undefined) { + if ((this.arr[0] as Array)[0] !== undefined) { // 正常情况下需要有一个真实的ID来与ForEach一起使用,但此处没有 // 因此需要确保推送的字符串是唯一的。 this.arr[0].push(`${this.arr[0].slice(-1).pop()}${this.arr[0].slice(-1).pop()}`); diff --git a/zh-cn/application-dev/quick-start/arkts-persiststorage.md b/zh-cn/application-dev/quick-start/arkts-persiststorage.md index c92158b30875d0452ae9311055f7c8dd065916d6..7151d5d4c8106ad48473fadf55c2e9f41774f3b8 100644 --- a/zh-cn/application-dev/quick-start/arkts-persiststorage.md +++ b/zh-cn/application-dev/quick-start/arkts-persiststorage.md @@ -40,7 +40,7 @@ PersistentStorage和UIContext相关联,需要在[UIContext](../reference/apis/ 2. 在AppStorage获取对应属性: ```ts - AppStorage.Get('aProp'); // returns 47 + AppStorage.Get('aProp'); // returns 47 ``` 或在组件内部定义: diff --git a/zh-cn/application-dev/quick-start/arkts-prop.md b/zh-cn/application-dev/quick-start/arkts-prop.md index b855989d323ffdf8d6d5905874a3c68918c197a8..9250154d01eb42c8c6c56c47b30dc586621f0b00 100644 --- a/zh-cn/application-dev/quick-start/arkts-prop.md +++ b/zh-cn/application-dev/quick-start/arkts-prop.md @@ -118,7 +118,7 @@ this.title.push('3') ```ts @Component struct DateComponent { - @Prop selectedDate: Date; + @Prop selectedDate: Date = new Date(''); build() { Column() { @@ -198,7 +198,7 @@ ParentComponent的状态变量countDownStartValue的变化将重置CountDownComp ```ts @Component struct CountDownComponent { - @Prop count: number; + @Prop count: number = 0; costOfOneAttempt: number = 1; build() { @@ -370,7 +370,7 @@ class Book { @Component struct ReaderComp { - @Prop book: Book; + @Prop book: Book = new Book("", 0); build() { Row() { @@ -419,7 +419,7 @@ class Book { @Component struct ReaderComp { - @Prop book: Book; + @Prop book: Book = new Book("", 1); build() { Row() { @@ -442,10 +442,10 @@ struct Library { ReaderComp({ book: this.allBooks[2] }) Divider() Text('Books on loaan to a reader') - ForEach(this.allBooks, book => { + ForEach(this.allBooks, (book: Book) => { ReaderComp({ book: book }) }, - book => book.id) + (book: Book) => book.id.toString()) Button('Add new') .onClick(() => { this.allBooks.push(new Book("The C++ Standard Library", 512)); @@ -497,7 +497,7 @@ class Book { ```ts @Component struct MyComponent { - @Prop customCounter: number; + @Prop customCounter: number = 0; @Prop customCounter2: number = 5; build() { @@ -579,7 +579,6 @@ class ClassB { 以下组件层次结构呈现的是@Prop嵌套场景的数据结构。 ```ts - @Entry @Component struct Parent { @@ -588,10 +587,10 @@ struct Parent { build() { Column() { Button('change') - .onClick(() => { - this.votes.name = "aaaaa" - this.votes.a.title = "wwwww" - }) + .onClick(() => { + this.votes.name = "aaaaa" + this.votes.a.title = "wwwww" + }) Child({ vote: this.votes }) } @@ -600,33 +599,33 @@ struct Parent { @Component struct Child { - @Prop vote: ClassB + @Prop vote: ClassB = new ClassB('', new ClassA('')); build() { - Column() { + Column() { - Text(this.vote.name).fontSize(36).fontColor(Color.Red).margin(50) - .onClick(() => { - this.vote.name = 'Bye' - }) - Text(this.vote.a.title).fontSize(36).fontColor(Color.Blue) - .onClick(() => { - this.vote.a.title = "openHarmony" - }) - Child1({vote1:this.vote.a}) + Text(this.vote.name).fontSize(36).fontColor(Color.Red).margin(50) + .onClick(() => { + this.vote.name = 'Bye' + }) + Text(this.vote.a.title).fontSize(36).fontColor(Color.Blue) + .onClick(() => { + this.vote.a.title = "openHarmony" + }) + Child1({vote1:this.vote.a}) - } + } } } @Component struct Child1 { - @Prop vote1: ClassA + @Prop vote1: ClassA = new ClassA(''); build() { Column() { - Text(this.vote1.title).fontSize(36).fontColor(Color.Red).margin(50) - .onClick(() => { - this.vote1.title = 'Bye Bye' - }) + Text(this.vote1.title).fontSize(36).fontColor(Color.Red).margin(50) + .onClick(() => { + this.vote1.title = 'Bye Bye' + }) } } }