HarmonyOS 鸿蒙Next不支持function.bind
HarmonyOS 鸿蒙Next不支持function.bind
“Function.bind” is not supported (arkts-no-func-bind) <ArkTSCheck>
3 回复
arkts-no-func-bind当前应该是warn级别告警,不是error级别,还是可以用的。
只是你这里应该是想弹窗调用父组件的函数,我这边试,this.startGame为了调用bind,把startGam的()去掉了,导致弹框调用startGame不生效。去掉bind(this),直接传this.startGame()是ok的。
示例代码:
// xxx.ets
[@CustomDialog](/user/CustomDialog)
struct CustomDialogExample {
controller?: CustomDialogController
add: () => void = () => {
}
build() {
Column() {
Button('加1')
.onClick(() => {
this.add()
})
Button('点我关闭弹窗')
.onClick(() => {
if (this.controller != undefined) {
this.controller.close()
}
})
.margin(20)
}
}
}
[@Entry](/user/Entry)
[@Component](/user/Component)
struct CustomDialogUser {
dialogController: CustomDialogController | null = new CustomDialogController({
builder: CustomDialogExample({
add: () => {
this.add()
}
}),
autoCancel: true,
onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
console.log("dialog onWillDismiss")
if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
dismissDialogAction.dismiss()
}
if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
dismissDialogAction.dismiss()
}
},
alignment: DialogAlignment.Center,
offset: { dx: 0, dy: -20 },
customStyle: false,
cornerRadius: 20,
width: 300,
height: 200,
borderWidth: 1,
borderStyle: BorderStyle.Dashed, //使用borderStyle属性,需要和borderWidth属性一起使用
borderColor: Color.Blue, //使用borderColor属性,需要和borderWidth属性一起使用
backgroundColor: Color.White,
shadow: ({
radius: 20,
color: Color.Grey,
offsetX: 50,
offsetY: 0
}),
})
// 在自定义组件即将析构销毁时将dialogController置空
aboutToDisappear() {
this.dialogController = null // 将dialogController置空
}
add() {
this.num = this.num + 1
}
[@State](/user/State) num: number = 1
build() {
Column() {
Button('click me')
.onClick(() => {
if (this.dialogController != null) {
this.dialogController.open()
}
}).backgroundColor(0x317aff)
Text(this.num.toString())
}.width('100%').margin({ top: 5 })
}
}