HarmonyOS 鸿蒙Next官方指南代码也报错(this问题)
HarmonyOS 鸿蒙Next官方指南代码也报错(this问题)
官方指南实例代码,应该怎么改?把变量提出去,然后删除this吗?
更多关于HarmonyOS 鸿蒙Next官方指南代码也报错(this问题)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
示例中的可执行的可以用下面的代码来执行:
1、Index.ets
// Index.ets
import { ComponentContent } from '@kit.ArkUI';
import { PromptActionClass } from './PromptActionClass';
class Params {
text: string = ""
constructor(text: string) {
this.text = text;
}
}
@Builder
function buildText(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
Button('Close')
.onClick(() => {
PromptActionClass.closeDialog()
})
}.backgroundColor('#FFF0F0F0')
}
class point {
dx: number = 0;
dy: number = 0;
constructor(x:number, y:number) {
this.dx = x;
this.dy = y
}
};
class obj {
alignment: DialogAlignment = DialogAlignment.Top;
a:point = new point(0, 0)
constructor(al: DialogAlignment, p:point) {
this.alignment = al;
this.a = p;
}
}
@Entry
@Component
struct Index {
@State message: string = "hello"
private ctx: UIContext = this.getUIContext();
private contentNode: ComponentContent<Object> =
new ComponentContent(this.ctx, wrapBuilder(buildText), new Params(this.message));
aboutToAppear(): void {
PromptActionClass.setContext(this.ctx);
PromptActionClass.setContentNode(this.contentNode);
PromptActionClass.setOptions(
new obj(DialogAlignment.Top, new point(0, 50))
);//{ alignment: DialogAlignment.Top, offset: { dx: 0, dy: 50 } }
}
build() {
Row() {
Column() {
Button("open dialog and update options")
.margin({ top: 50 })
.onClick(() => {
PromptActionClass.openDialog()
setTimeout(() => {
PromptActionClass.updateDialog(
new obj(DialogAlignment.Bottom, new point(0, -50))
) // {alignment: DialogAlignment.Bottom,offset: { dx: 0, dy: -50 }}
}, 1500)
})
Button("open dialog and update content")
.margin({ top: 50 })
.onClick(() => {
PromptActionClass.openDialog()
setTimeout(() => {
this.contentNode.update(new Params('update'))
}, 1500)
})
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
2、PromptActionClass.ets
// PromptActionClass.ts
import { BusinessError } from '@kit.BasicServicesKit';
import { ComponentContent, window } from '@kit.ArkUI';
import { UIContext } from '@ohos.arkui.UIContext';
export class PromptActionClass {
static ctx: UIContext;
static contentNode: ComponentContent<Object>;
static options: Object;
static setContext(context: UIContext) {
PromptActionClass.ctx = context;
}
static setContentNode(node: ComponentContent<Object>) {
PromptActionClass.contentNode = node;
}
static setOptions(options: Object) {
PromptActionClass.options = options;
}
static openDialog() {
if (PromptActionClass.contentNode !== null) {
PromptActionClass.ctx.getPromptAction().openCustomDialog(PromptActionClass.contentNode, PromptActionClass.options)
.then(() => {
console.info('OpenCustomDialog complete.')
})
.catch((error: BusinessError) => {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
})
}
}
static closeDialog() {
if (PromptActionClass.contentNode !== null) {
PromptActionClass.ctx.getPromptAction().closeCustomDialog(PromptActionClass.contentNode)
.then(() => {
console.info('CloseCustomDialog complete.')
})
.catch((error: BusinessError) => {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`CloseCustomDialog args error code is ${code}, message is ${message}`);
})
}
}
static updateDialog(options: Object) {
if (PromptActionClass.contentNode !== null) {
PromptActionClass.ctx.getPromptAction().updateCustomDialog(PromptActionClass.contentNode, options)
.then(() => {
console.info('UpdateCustomDialog complete.')
})
.catch((error: BusinessError) => {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`UpdateCustomDialog args error code is ${code}, message is ${message}`);
})
}
}
}
更多关于HarmonyOS 鸿蒙Next官方指南代码也报错(this问题)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
报这个错误是由于不支持在独立的函数中使用this指针,比如举个例子:
class Test {
static num: number;
static get() {
this.num = 100
return this.num;
}
}
可以改成:
class Test { static num: number;
static get() { Test.num = 100 return Test.num; } }
在例子中,将class PromptActionClass中的this.都改成PromptActionClass.即可。
针对您提到的HarmonyOS 鸿蒙Next官方指南中的代码报错(this问题),通常这类问题可能涉及以下几个方面:
-
作用域问题:确认
this
关键字的使用是否在正确的作用域内,如是否在类的成员函数中,因为this
通常用于引用当前对象的实例。 -
语法错误:检查
this
的使用是否符合鸿蒙系统的语法规范,比如是否有拼写错误或者在不支持this
的上下文(如静态方法中)使用。 -
对象状态:确保在使用
this
时,当前对象已被正确初始化,未处于未定义或null状态。 -
编译器版本:确认您的开发环境(包括编译器和SDK)是否支持您正在使用的鸿蒙系统版本,有时候版本不匹配也会导致编译错误。
-
官方示例代码:重新核对官方指南中的示例代码,确保没有遗漏或误解任何关键步骤或注释。
如果以上检查后问题依旧存在,可能涉及到更具体的错误信息或代码上下文。建议直接查看编译器的错误输出,获取更详细的错误信息。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。