1. 云对象使用
按照文档给出代码,直接使用会报错,原因为res没有给定类型,鸿蒙要求所有变量必须指定类型,例如:
interface Obj {
result: number
}
let myCloudObject = importObject(MyCloudObject);
myCloudObject.add(1, 2).then((addResult: Obj) => {
console.log(`1 + 2 = ${addResult.result}`);
});
(PS:serverless服务不支持模拟器调试,所以如果没有真机的建议不要使用了)
2. 显示当前日期时间
文档的代码模拟器运行有些问题,可参考:
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
@State time: string = '';
aboutToAppear(): void {
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDay();
let hour = date.getHours();
let mins = date.getMinutes();
let seconds = date.getSeconds();
this.time = `${year}-${month}-${day} ${hour}:${mins}:${seconds}`;
}
build() {
RelativeContainer() {
Text(this.time)
.id('HelloWorld')
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
});
}
.height('100%')
.width('100%');
}
}
3. 其他碰到的问题
官方的软件包加密可能会导致软件异常,本地配置的软件包加密如果使用记得配置白名单,特别是一些写死的变量,否则软件会异常