鴻蒙自定義彈窗中的變量如何傳遞給頁面

鴻蒙自定義彈窗中的變量如何傳遞給頁面

方式一:使用組件的狀態(tài)變量傳遞。

方式二:在初始化彈窗時,傳遞一個方法給自定義彈窗。

方式三:使用AppStorage或LocalStorage方式管理頁面狀態(tài),實現(xiàn)自定義彈窗和頁面之間狀態(tài)的共享。


方式一:使用組件的狀態(tài)變量傳遞。

@CustomDialog

export struct CustomDialog01 {

? @Link inputValue: string

? controller: CustomDialogController

? build() {

? ? Column() {

? ? ? Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })

? ? ? TextInput({ placeholder: '', text: this.inputValue }).height(60).width('90%')

? ? ? ? .onChange((value: string) => {

? ? ? ? ? this.inputValue = value

? ? ? ? })

? ? }

? }

}

@Entry

@Component

struct DialogDemo01 {

? @State inputValue: string = 'click me'

? dialogController: CustomDialogController = new CustomDialogController({

? ? builder: CustomDialog01({

? ? ? inputValue: $inputValue

? ? })

? })

? build() {

? ? Column() {

? ? ? Button(this.inputValue).fontSize(30)

? ? ? ? .onClick(() => {

? ? ? ? ? this.dialogController.open()

? ? ? ? }).backgroundColor(0x317aff)

? ? }.width('100%').margin({ top: 5 }).height("100%").justifyContent(FlexAlign.Center)

? }

}



方式二:在初始化彈窗時,傳遞一個方法給自定義彈窗,在自定義彈窗中觸發(fā)該方法,彈窗中變量作為方法的參數(shù)。

@CustomDialog

export? struct CustomDialog01 {

? private inputValue: string

? changeInputValue: (val: string) => void

? controller: CustomDialogController

? build() {

? ? Column() {

? ? ? Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })

? ? ? TextInput({ placeholder: '', text: this.inputValue }).height(60).width('90%')

? ? ? ? .onChange((value: string) => {

? ? ? ? ? this.changeInputValue(value)

? ? ? ? })

? ? }

? }

}

@Entry

@Component

struct DialogDemo01 {

? @State inputValue: string = 'click me'

? dialogController: CustomDialogController = new CustomDialogController({

? ? builder: CustomDialog01({

? ? ? inputValue: this.inputValue,

? ? ? changeInputValue: (val: string) => {

? ? ? ? this.inputValue = val

? ? ? }

? ? })

? })

? build() {

? ? Column() {

? ? ? Button(this.inputValue).fontSize(30)

? ? ? ? .onClick(() => {

? ? ? ? ? this.dialogController.open()

? ? ? ? }).backgroundColor(0x317aff)

? ? }.width('100%').margin({ top: 5 }).height("100%").justifyContent(FlexAlign.Center)

? }

}


方式三:使用AppStorage或LocalStorage方式管理頁面狀態(tài),實現(xiàn)自定義彈窗和頁面之間狀態(tài)的共享

let storage = LocalStorage.GetShared()

@CustomDialog

export? struct CustomDialog01 {

? @LocalStorageLink('inputVal')? inputValue: string = ''

? controller: CustomDialogController

? build() {

? ? Column() {

? ? ? Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })

? ? ? TextInput({ placeholder: '', text: this.inputValue }).height(60).width('90%')

? ? ? ? .onChange((value: string) => {

? ? ? ? ? this.inputValue = value;

? ? ? ? })

? ? }

? }

}

@Entry(storage)

@Component

struct DialogDemo01 {

? @LocalStorageLink('inputVal') inputValue: string = '點擊'

? dialogController: CustomDialogController = new CustomDialogController({

? ? builder: CustomDialog01()

? })

? build() {

? ? Column() {

? ? ? Button(this.inputValue)

? ? ? ? .onClick(() => {

? ? ? ? ? this.dialogController.open()

? ? ? ? }).backgroundColor(0x317aff)

? ? }.width('100%').margin({ top: 5 })

? }

}


以上就是 自定義彈窗中的變量如何傳遞給頁面的三種辦法。

鴻蒙自定義彈窗中的變量如何傳遞給頁面

https://blog.51cto.com/jianguo/9057664

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容