vue 开发中常见 ts 问题及解决方案

1,定义对象后扩展对象属性报错

报错详情:Property 'id' does not exist on type 'query'.Vetur(2339)

例如:

let query = {name: 'aa'}
if (id) {
    query.id = id // Property 'id' does not exist on type 'query'.Vetur(2339)
}

解决方法,添加代码:

interface queryObject {
    name: String
    [prop: string]: any
}
let query: queryObject  = {name: 'aa'}
if (id) {
    query.id = id
}

评论

暂无评论