HarmonyOS 鸿蒙Next ets疑难杂症
HarmonyOS 鸿蒙Next ets疑难杂症 只要在foreach 方法下面写if就报错,其他电脑正常,只有我的电脑才会,这是什么插件检查问题?
@Entry
@Component
struct SecondPage {
@State message: string = 'Hello World'
luckyName: string[] = ["P50", "感谢老板", "平板", "现金500", "开始抽奖", "Q币40", "身体健康", "Iphone14", "谢谢参与"]
//用于旋转下标
@State currentIndex: number = 0
//这是真实选中的下标
@State selectIndex: number = 0
build() {
Flex({ direction: FlexDirection.Row,
justifyContent: FlexAlign.SpaceAround,
wrap: FlexWrap.Wrap,
alignContent: FlexAlign.SpaceAround
}) {
//动态添加
ForEach(this.luckyName, (item, index) => {
if (index == 4) {
//开始抽奖
Column() {
Button("开始抽奖")
.height(140)
.type(ButtonType.Normal)
.borderRadius(20)
.onClick(() => {
this.startLucky()
})
}.width("30%")
} else {
//抽奖选项
Column() {
Image($r("app.media.icon"))
.width(100)
.height(100)
Text(item)
.fontSize(18)
.fontWeight(FontWeight.Bold)
}.width("30%")
.backgroundColor(this.selectIndex == index ? Color.Pink : Color.White)
.padding(10)
.borderRadius(15)
}
})
}.backgroundColor("#ff91f391")
.height(500)
}
timerId: number = -1
//开始抽奖函数
startLucky() {
//floor 向下取整
this.stopCount = Math.floor(Math.random() * 10 + 50)
console.info(this.stopCount + "")
//定时执行
//1000 毫秒
this.timerId = setInterval(() => {
this.countDownTimer()
}, 80)
}
//当前转动的圈数
count: number = 0
//停止的圈数
stopCount: number = 50
//倒计时停止
countDownTimer() {
this.count++
this.currentIndex++
if (this.currentIndex > 7) {
this.currentIndex = 0
}
this.currentIndexToSelectIndex()
console.info(this.currentIndex + "")
if (this.count == 20) {
//开始减速
//停止 以前的任务
clearInterval(this.timerId)
this.timerId = setInterval(() => {
this.countDownTimer()
}, 150)
}
if (this.count == this.stopCount) {
//停止 抽奖完毕
clearInterval(this.timerId)
//重置
this.count = 0
}
}
//将下标进行转换
currentIndexToSelectIndex() {
switch (this.currentIndex) {
case 0:
this.selectIndex = 0;
break
case 1:
this.selectIndex = 1;
break
case 2:
this.selectIndex = 2;
break
case 3:
this.selectIndex = 5;
break
case 4:
this.selectIndex = 8;
break
case 5:
this.selectIndex = 7;
break
case 6:
this.selectIndex = 6;
break
case 7:
this.selectIndex = 3;
break
case 8:
this.selectIndex = 0;
break
}
}
}
更多关于HarmonyOS 鸿蒙Next ets疑难杂症的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
开发者您好,该问题无法复现,请提供下IDE版本。
更多关于HarmonyOS 鸿蒙Next ets疑难杂症的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
是不是用的是DevEco Studio 3.0 Release版本,原来的Beta版本没有问题,升级到Release才出现的,只是飘红报错,可以正常运行。