HarmonyOS 鸿蒙Next遇到class constructor cannot called without 'new',但检查后并没有发现缺少new的关键字
HarmonyOS 鸿蒙Next遇到class constructor cannot called without ‘new’,但检查后并没有发现缺少new的关键字
import { CommonConstants } from '../../common/constants/CommonConstants'
import DateUtil from '../../common/utils/DateUtil'
import DatepickDialog from './DatePickDialog'
@Component
export default struct StaticCard {
@StorageProp('selectedDate') selectedDate:number=DateUtil.beginTimeOfDay(new Date())
controller:CustomDialogController= new CustomDialogController({
builder:DatepickDialog({selectedDate:new Date(this.selectedDate)})
})
build() {
Column(){
//1.日期
Row(){
Text(DateUtil.formatDate(this.selectedDate)).fontColor($r('app.color.secondary_color'))
Image($r('app.media.ic_public_spinner')).width(20).fillColor($r('app.color.secondary_color'))
}.padding(CommonConstants.SPACE_8)
//2.统计信息
.onClick(()=>{
this.controller.open()
})
}
.width(CommonConstants.THOUSANDTH_940)
.backgroundColor($r('app.color.stats_title_bgc'))
.borderRadius(CommonConstants.DEFAULT_18)
}
}
import { CommonConstants } from '../../common/constants/CommonConstants'
@Component
export default struct DatepickDialog {
controller:CustomDialogController
selectedDate:Date=new Date()
build() {
Column({space:CommonConstants.SPACE_12}){
//1.日期选择
DatePicker({
start: new Date('2020-01-01'),
end: new Date(),
selected: this.selectedDate
})
.onChange((value: DatePickerResult) => {
this.selectedDate.setFullYear(value.year, value.month, value.day)
})
//2.按钮
Row({space:CommonConstants.SPACE_12}){
Button('取消').width(120).backgroundColor($r('app.color.light_gray'))
.onClick(()=>{
this.controller.close()
})
Button('提交').width(120).backgroundColor($r('app.color.primary_color'))
.onClick(()=>{
//1.保存日期到全局
AppStorage.SetOrCreate('selectedDate',this.selectedDate.getTime())
this.controller.close()
})
}
}.padding(CommonConstants.SPACE_12)
}
}
为什么在调用
controller: CustomDialogController = new CustomDialogController({ builder: DatePickDialog({selectedDate: new Date(this.selectedDate)}) })后,我的日期组件就消失了,在preview里面不显示,注解掉这段就正常了,log里面报错class constructor cannot called without ‘new’
10 回复
有大佬帮忙看一下嘛?
预览模拟器中显示不了这个效果
好像不是这个原因,同学同样的代码可以显示出来
+1
类似的问题,我是写了一个子类继承了CustomDialogController,封装了一些通用设置,结果也是提示这个错误。。。。
我遇到了跟你一样的问题,最后你是怎么处理的
把弹框上面的[@Component](/user/Component)改成[@CustomDialog](/user/CustomDialog)
特地登录了来给你点赞,这谁想得到
牛啊