自定义弹窗中的变量如何传递给页面?

自定义弹窗内定义的变量内容,在关闭弹窗或变量变化时需要及时传递给页面,可以通过何种方式传递?

阅读 819
avatarAI BotBETA

您可以使用以下几种方式将自定义弹窗中的变量传递给页面:

  1. 使用回调函数:在自定义弹窗中定义一个回调函数,该函数将接收您需要传递的变量。当用户关闭弹窗时,回调函数将被调用,并将变量传递回页面。
  2. 使用全局变量:在自定义弹窗中定义一个全局变量,并在页面中引用该变量。当变量在自定义弹窗中发生变化时,它将在页面中更新。
  3. 使用 Web Storage API:Web Storage API(如 localStorage 或 sessionStorage)允许您在浏览器中存储数据。您可以在自定义弹窗中存储变量,并在页面中检索它们。
  4. 使用 WebSocket:WebSocket 是一种允许在浏览器和服务器之间建立持久连接并交换数据的协议。您可以在自定义弹窗中使用 WebSocket 将变量发送到服务器,并在页面中接收它们。
  5. 使用 AJAX:AJAX 是一种在不重新加载整个页面的情况下,与服务器交换数据并更新部分网页的技术。您可以在自定义弹窗中使用 AJAX 将变量发送到服务器,并在页面中接收它们。

根据您的具体需求和情况,选择最适合您的方法。

1 个回答

可以使用以下三种方式来实现:

1.使用组件的状态变量传递。

@CustomDialog 
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) 
        .onClick(() => { 
          this.dialogController.open() 
        }).backgroundColor(0x317aff) 
    }.width('100%').margin({ top: 5 }) 
  } 
}

2.在初始化弹窗时,传递一个方法给自定义弹窗,在自定义弹窗中触发该方法,弹窗中变量作为方法的参数。

@CustomDialog 
struct CustomDialog02 { 
  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 DialogDemo02 { 
  @State inputValue: string = 'click me'; 
  dialogController: CustomDialogController = new CustomDialogController({ 
    builder: CustomDialog02({ 
      inputValue: this.inputValue, 
      changeInputValue: (val: string) => { 
        this.inputValue = val 
      } 
    }) 
  }); 
 
  build() { 
    Column() { 
      Button(this.inputValue) 
        .onClick(() => { 
          this.dialogController.open(); 
        }).backgroundColor(0x317aff) 
    }.width('100%').margin({ top: 5 }) 
  } 
}

3.使用AppStorage或LocalStorage方式管理页面状态,实现自定义弹窗和页面之间状态的共享。

let storage = LocalStorage.getShared(); 
 
@CustomDialog 
struct CustomDialog03 { 
  @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 DialogDemo03 { 
  @LocalStorageLink('inputVal') inputValue: string = ''; 
  dialogController: CustomDialogController = new CustomDialogController({ 
    builder: CustomDialog03() 
  }); 
 
  build() { 
    Column() { 
      Button(this.inputValue) 
        .onClick(() => { 
          this.dialogController.open(); 
        }).backgroundColor(0x317aff) 
    }.width('100%').margin({ top: 5 }) 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进