文档管理中心
我的
|

HarmonyOS 5

关于项目频繁出现的Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)

component 组件

收起
自动换行
深色代码主题
复制
@Component
export default struct PostPage {
  @State postList:PostItemEntity[]=[]
  aboutToAppear(): void {
    const obj:PostItemEntity={
      id: 1,
      postText: "既然选择远⽅,当不负⻘春,砥砺前⾏",
      rightCount: 3,
      answeredCount: 4,
      timeUsed: 5747,
      createTime: "2024-03-19 18:54:33",
      likeCount: 1,
      nickname: "138****8888",
      avatarUrl: "https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg",
      isLike: false
    }
    this.postList.push(obj)
  }
  build() {

  }
}

类型

收起
自动换行
深色代码主题
复制
interface PostItem {
  id: number;
  postText: string;
  rightCount: number;
  answeredCount: number;
  timeUsed: number;
  createTime: string;
  likeCount: number;
  nickname: string;
  avatarUrl: string;
  isLike: boolean;
}

@Observed
export class PostItemEntity implements PostItem {
  public id: number;
  public postText: string;
  public rightCount: number;
  public answeredCount: number;
  public timeUsed: number;
  public createTime: string;
  public likeCount: number;
  public nickname: string;
  public avatarUrl: string;
  public isLike: boolean;

  constructor(id: number,
    postText: string,
    rightCount: number,
    answeredCount: number,
    timeUsed: number,
    createTime: string,
    likeCount: number,
    nickname: string,
    avatarUrl: string,
    isLike: boolean) {
    this.id = id;
    this.postText = postText;
    this.rightCount = rightCount;
    this.answeredCount = answeredCount;
    this.timeUsed = timeUsed;
    this.createTime = createTime;
    this.likeCount = likeCount;
    this.nickname = nickname;
    this.avatarUrl = avatarUrl;
    this.isLike = isLike;
  }
}

这个push报错

点赞
收藏
回复
1
分享
举报
浏览997 发布于2024-12-16 09:41湖北
全部评论
最多点赞
最新发布
最早发布
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。