国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

做網站重要標簽成都seo優(yōu)化排名推廣

做網站重要標簽,成都seo優(yōu)化排名推廣,室內裝修效果圖,上海高檔網站建設組件狀態(tài)管理 一、State State用于裝飾當前組件的狀態(tài)變量,State裝飾的變量在發(fā)生變化時,會驅動當前組件的視圖刷新,語法如下: State count:number 1; 需要注意的是:State裝飾的變量必須進行本地初始化。 允許裝…

組件狀態(tài)管理

一、@State

@State用于裝飾當前組件的狀態(tài)變量,@State裝飾的變量在發(fā)生變化時,會驅動當前組件的視圖刷新,語法如下:

@State count:number = 1;


需要注意的是:@State裝飾的變量必須進行本地初始化。

允許裝飾的類型

基本類型:string、number、boolean、enum

對象類型:json對象、class的實例對象、

數組類型:上面所有類型的數組

能夠觀察到的變化

注意:

并不是狀態(tài)變量的所有更改都會引起UI的刷新

只有可以被框架觀察到的修改才會引起UI刷新

屬性本身的改變都可以 (無論什么類型)

對象:能監(jiān)視對象的直接屬性變化,不能監(jiān)視嵌套屬性的改變

數組:能監(jiān)視數組中元素的變化,不能監(jiān)視元素對象內部的變化

測試:

定義對象和數組的狀態(tài)和顯示數據

@State @Watch('onChange')obj: {a: {b: number}} = {a: {b: 1}}
@State @Watch('onChange2')arr: {a: number}[] = [{a: 1}]
Text('state.obj' + JSON.stringify(this.obj)).fontSize(18)
Text('state.arr' + JSON.stringify(this.arr)).fontSize(18)

修改屬性對象

this.obj = {a: {b: 2}}   // 修改屬性本身 => 能監(jiān)視
this.obj.a = {b: 3}  // 修改屬性對象的直接屬性 =》 能監(jiān)視
this.obj.a.b = 4   // 修改屬性對象的嵌套屬性 =》 不能監(jiān)視到

修改屬性數組

this.arr = [] // 修改屬性本身 => 能監(jiān)視
this.arr[0] = {a: 2} // 修改屬性數組的元素 => 能監(jiān)視
this.arr.push({a: 3}) // 修改屬性數組的元素 => 能監(jiān)視
this.arr[0].a = 4 // 修改屬性數組的元素對象內部屬性 =》 不能監(jiān)視

二、@Prop

@Prop用于裝飾子組件的狀態(tài)變量,@Prop裝飾的變量會同步父組件的狀態(tài),但只能單向同步,也就是父組件的狀態(tài)變化會自動同步到子組件,而子組件的變化不會同步到父組件

父組件

@Entry
@Component
struct Parent{@State count:number = 1;build(){Column(){Child({count:this.count});}}
}

子組件

@Component
export struct Child{@Prop count:number;build(){Text('prop.count: ' + this.count);}
}

需要注意的是:@Prop裝飾的變量不允許本地初始化,只能通過父組件傳參進行初始化。

允許裝飾的類型

官方文檔:只允許基本類型,不允許對象和數組

實際情況:與@State一致,可以是對象、數組

能夠觀察到的變化

與@State一致

三、@Link

@Link用于裝飾子組件的狀態(tài)變量,@Prop變量同樣會同步父組件狀態(tài),但是能夠雙向同步。也就是父組件的變化會同步到子組件,而子組件的變化也會同步到父組件

父組件

@Entry
@Component
struct Parent{@State count:number = 1;build(){Column(){Child({count: $count});}}
}

子組件

@Component
export struct Child{@Link count:number;build(){Text('link.count: ' + this.count);}
}

需要注意的是:@Link裝飾的變量不允許本地初始化,只能由父組件通過傳參進行初始化,并且父組件必須使用$變量名的方式傳參,以表示傳遞的是變量的引用。

允許裝飾的類型

與@State一致

框架能夠觀察到的變化

與@State一致

四、@Provide 與 @Consume

@Provide和@Consume用于跨層級傳遞狀態(tài)信息,其中@Provide用于裝飾祖先組件的狀態(tài)變量,@Consume用于裝飾后代組件的狀態(tài)變量。可以理解為祖先組件提供(Provide)狀態(tài)信息供后代組件消費(Consume),并且祖先和后代的狀態(tài)信息可以實現雙向同步。

注意:

@Provide裝飾變量必須本地初始化,而@Consume裝飾的變量不允許本地初始化。

@Provide & @Consume處理的狀態(tài)數據是雙向同步的

祖先組件

@Entry
@Component
struct GrandParent {@Provide count: number = 1;@Provide('msg') message: string = '老A';build() {Column() {...}}
}

后代組件

@Entry
@Component
export struct Child {@Consume count: number;@Consume('msg') childMsg: string;build() {Column() {Text('Consume.count: ' + this.count);Text('Consume.childMsg: ' + this.childMsg);}}
}

允許裝飾的類型

與@State一致

能夠觀察到的變化

與@State一致

測試:

@Component
export default struct Child1 {@Prop obj1: {a: {b: number}}@Prop arr1: {a: number}[]update() {// this.obj1 = {a: {b: 3}}   // 修改屬性本身 => 能監(jiān)視// this.obj1.a = {b: 4}  // 修改屬性對象的直接屬性 =》 能監(jiān)視// setTimeout(() => {//   this.obj1.a.b = 9   // 修改屬性對象的嵌套屬性 =》 不能監(jiān)視到 報錯(且會報錯,導致程序退出)// }, 1000)// this.arr1 = [] // 修改屬性本身 => 能監(jiān)視// this.arr1[0] = {a: 5} // 修改屬性數組的元素 => 能監(jiān)視this.arr1.push({a: 8}) // 修改屬性數組的元素 => 能監(jiān)視setTimeout(() => {this.arr1[0].a = 5 // 修改屬性數組的元素對象內部屬性 =》 不能監(jiān)視(且會報錯,導致程序退出)}, 1000)}build() {Column({space: 10}) {Text('prop.obj' + JSON.stringify(this.obj1)).fontSize(18)Text('prop.arr' + JSON.stringify(this.arr1)).fontSize(18)Button('開始更新 prop').onClick(() => this.update())}.width('100%').padding(20).border({width: 1, color: Color.Gray})}
}
@Component
export default struct Child2 {@Link obj2: {a: {b: number}}@Link arr2: {a: number}[]update () {// this.obj1 = {a: {b: 2}}   // 修改屬性本身 => 能監(jiān)視// this.obj2.a = {b: 4}  // 修改屬性對象的直接屬性 =》 能監(jiān)視// setTimeout(() => {//   this.obj2.a.b = 9   // 修改屬性對象的嵌套屬性 =》 不能監(jiān)視到// })// this.arr2 = [] // 修改屬性本身 => 能監(jiān)視// this.arr2[0] = {a: 3} // 修改屬性數組的元素 => 能監(jiān)視this.arr2.push({a: 5}) // 修改屬性數組的元素 => 能監(jiān)視setTimeout(() => {this.arr2[0].a = 4 // 修改屬性數組的元素對象內部屬性 =》 不能監(jiān)視})}build() {Column({space: 10}) {Text('link.obj2' + JSON.stringify(this.obj2)).fontSize(18)Text('link.arr2' + JSON.stringify(this.arr2)).fontSize(18)Button('開始更新 link').onClick(() => this.update())}.width('100%').padding(20).border({width: 1, color: Color.Gray})}
}
import Child1 from './Child1'
import Child2 from './Child2'
@Entry
@Component
struct StateTest {@State obj: {a: {b: number}} = {a: {b: 1}}@State arr: {a: number}[] = [{a: 1}]update() {// this.obj = {a: {b: 1}}   // 修改屬性本身 => 能監(jiān)視// this.obj.a = {b: 2}  // 修改屬性對象的直接屬性 =》 能監(jiān)視// setTimeout(() => {//   this.obj.a.b = 6   // 修改屬性對象的嵌套屬性 =》 不能監(jiān)視到// }, 1000)// this.arr = [] // 修改屬性本身 => 能監(jiān)視// this.arr[0] = {a: 2} // 修改屬性數組的元素 => 能監(jiān)視this.arr.push({a: 3}) // 修改屬性數組的元素 => 能監(jiān)視setTimeout(() => {this.arr[0].a = 9 // 修改屬性數組的元素對象內部屬性 =》 不能監(jiān)視}, 1000)}build() {Column({space: 10}) {Text('state.obj' + JSON.stringify(this.obj)).fontSize(18)Text('state.arr' + JSON.stringify(this.arr)).fontSize(18)Button('開始更新2 state').onClick(() => this.update())Child1({obj1: this.obj, arr1: this.arr})Child2({obj2: $obj, arr2: $arr})}.width('100%').padding(20)}
}

五、@Watch

用來監(jiān)視狀態(tài)數據的變化,包括:@State、@Prop、@Link、@Provide、@Consume

一旦狀態(tài)數據變化,監(jiān)視的回調就會調用

我們可以在監(jiān)視的回調中執(zhí)行應用需要的特定邏輯

以@State為例編碼

@State @Watch('onCountChange') count: number = 0
/*** 一旦count變化,此回調函數就會自動調用* @param name  被監(jiān)視的狀態(tài)屬性名*/
onCountChange (name) {// 可以在此做特定處理
}

測試

@Component
export default struct Child1 {@Prop count1: numberbuild() {Column({space: 10}) {Row({space: 10}) {Text('prop.count1: ' + this.count1).fontSize(18)Button('更新prop.count1').onClick(() => this.count1 += 1)}}.width('100%').padding(20).border({width: 1, color: Color.Gray})}
}
@Component
export default struct Child2 {@Link count2: numberbuild() {Column({space: 10}) {Row({space: 10}) {Text('link.count2: ' + this.count2).fontSize(18)Button('開始更新link.count2').onClick(() => this.count2 += 1)}}.width('100%').padding(20).border({width: 1, color: Color.Gray})}
}
import GrandChild from './GrandChild'
@Component
export default struct Child3 {build() {Column({space: 10}) {GrandChild()}.width('100%').padding(20).border({width: 1, color: Color.Gray})}
}
import promptAction from '@ohos.promptAction'
@Component
export default struct GrandChild {@Consume @Watch('onMsgChange') msg: stringonMsgChange () {promptAction.showToast({message: this.msg})}build() {Column({space: 10}) {Text('Consume.msg: ' + this.msg).fontSize(18)Button('開始更新Consume.count2').onClick(() => this.msg += '--')}.width('100%').padding(20).border({width: 1, color: Color.Gray})}
}
import Child1 from './Child1'
import Child2 from './Child2'
import promptAction from '@ohos.promptAction'
import Child3 from './Child3'
@Entry
@Component
struct StateBaseTest {@State @Watch('onCountChange')  count: number = 0@Provide msg: string = 'abc'/*** 一旦count變化,此回調函數就會自動調用* @param name  被監(jiān)視的狀態(tài)屬性名*/onCountChange (name) {if (this.count>3) {promptAction.showToast({message: `當前count為${this.count},已經超過了3`})}}build() {Column({space: 10}) {Row({space: 10}) {Text('state.count: ' + this.count).fontSize(18)Button('更新state.count').onClick(() => this.count += 1)}Text('count值超過3,每次更新都提示一下').fontColor(Color.Orange)Child1({count1: this.count})Child2({count2: $count})Divider()Text('provide.msg: ' + this.msg).fontSize(18)Button('開始更新provide.msg').onClick(() => this.msg += '++')Child3()}.width('100%').padding(20)}
}

六、@ObjectLink 和 @Observed

前面的問題:

● 屬性對象中的嵌套對象的屬性修改不能監(jiān)視到,也就不會自動更新UI

● 屬性數組中的元素對象的屬性修改不能監(jiān)視到,也就不會自動更新UI

● @Props與@Link聲明接收的屬性,必須是@State的屬性,而不能是@State屬性對象中嵌套的屬性

解決辦法

● 將嵌套對象的類型用class定義, 并使用@Observed來裝飾

● 子組件中定義的嵌套對象的屬性, 使用@ObjectLink來裝飾

測試:

@Observed
class Person2 {id: number;name: string;age: number;constructor(id, name, age) {this.id = idthis.name = namethis.age = age}
}
@Component
struct PersonItem {// @Prop person: Person// @Link person: Person@ObjectLink person: Person2build() {Row() {Text(JSON.stringify(this.person)).fontSize(20)Button('更新年齡').onClick(() => this.person.age += 2)}.border({width: 1, color: Color.Gray}).padding(10)}
}
@Entry
@Component
struct PersonList {@State persons: Person2[] = [new Person2(1, 'Tom', 12),new Person2(2, 'Jack', 13),]build() {Column({space: 10}){Button('更新嵌套對象的屬性:一個人的年齡').onClick(() => {this.persons[0].age++})List() {ForEach(this.persons, (item: Person2, index: number) => {ListItem(){PersonItem({person: item})}})}}.padding(20)}
}

以上就是常用組件狀態(tài)說明

為了幫助大家更深入有效的學習到鴻蒙開發(fā)知識點,小編特意給大家準備了一份全套最新版的HarmonyOS NEXT學習資源,獲取完整版方式請點擊→《HarmonyOS教學視頻

HarmonyOS教學視頻:語法ArkTS、TypeScript、ArkUI等.....視頻教程

鴻蒙生態(tài)應用開發(fā)白皮書V2.0PDF:

獲取完整版白皮書方式請點擊→《鴻蒙生態(tài)應用開發(fā)白皮書V2.0PDF》

鴻蒙 (Harmony OS)開發(fā)學習手冊

一、入門必看

  1. 應用開發(fā)導讀(ArkTS)
  2. ……

二、HarmonyOS 概念

  1. 系統(tǒng)定義
  2. 技術架構
  3. 技術特性
  4. 系統(tǒng)安全
  5. ........

三、如何快速入門?《做鴻蒙應用開發(fā)到底學些啥?》

  1. 基本概念
  2. 構建第一個ArkTS應用
  3. ……

四、開發(fā)基礎知識

  1. 應用基礎知識
  2. 配置文件
  3. 應用數據管理
  4. 應用安全管理
  5. 應用隱私保護
  6. 三方應用調用管控機制
  7. 資源分類與訪問
  8. 學習ArkTS語言
  9. ……

五、基于ArkTS 開發(fā)

  1. Ability開發(fā)
  2. UI開發(fā)
  3. 公共事件與通知
  4. 窗口管理
  5. 媒體
  6. 安全
  7. 網絡與鏈接
  8. 電話服務
  9. 數據管理
  10. 后臺任務(Background Task)管理
  11. 設備管理
  12. 設備使用信息統(tǒng)計
  13. DFX
  14. 國際化開發(fā)
  15. 折疊屏系列
  16. ……

更多了解更多鴻蒙開發(fā)的相關知識可以參考:《鴻蒙 (Harmony OS)開發(fā)學習手冊

http://m.aloenet.com.cn/news/31148.html

相關文章:

  • .net網站開發(fā)文檔教育培訓網站模板
  • 國家高新技術企業(yè)證書圖片北京seo分析
  • 程序員做博彩類的網站犯法嗎青島網站建設運營推廣
  • 河北高端網站定制公司seo營銷優(yōu)化軟件
  • 戴爾網站建設成功的關鍵網站怎么做收錄
  • wordpress數據庫導出網址鏈接關鍵詞推廣優(yōu)化外包
  • 浙江溫州最新消息鄭州seo外包公司哪家好
  • 怎樣開發(fā)公司的網站建設寧波seo怎么推廣
  • 清潔公司百度關鍵詞在線優(yōu)化
  • 動態(tài)網站開發(fā)工程師—aspseo一鍵優(yōu)化
  • 網站建設的需求怎么寫項目優(yōu)化seo
  • 寧波網站建設c nb網站優(yōu)化網
  • 全面做好政府網站建設管理工作的通知免費營銷培訓
  • 標識設計廠家珠海百度搜索排名優(yōu)化
  • 網站制作詳細流程凈水器十大品牌
  • 網站做視頻在線觀看網址網站開發(fā)合同
  • 陽江招聘網站哪個靠譜松原頭條新聞今日新聞最新
  • 廊坊網站建設技術外包百度平臺聯系方式
  • 網站頁腳信息網站播放視頻速度優(yōu)化
  • 電子商務網站建設預算微信公眾平臺開發(fā)
  • 公司網站購物平臺建設百度網盤網頁登錄入口
  • 網站a記錄的是做cname營銷方案怎么寫?
  • 免費的創(chuàng)建個人網站武漢大學人民醫(yī)院光谷院區(qū)
  • php做網站主要怎么布局百度明星人氣排行榜
  • 美容產品網站建設多少錢seo診斷網站
  • 手機h5頁面制作教程關鍵詞seo排名怎么選
  • 東京購物商城百度整站優(yōu)化
  • 技術支持 鄭州做網站企業(yè)網搭建
  • 旅游網站的導航怎么做陜西網站設計
  • 南寧網站建設醉懂網絡外鏈官網