【问题标题】:TypeScript Property 'push' does not exist on type '{}'类型“{}”上不存在 TypeScript 属性“推送”
【发布时间】:2019-02-24 09:05:20
【问题描述】:

这是我的代码。 实际上,我正在使用 firebase 服务器在 ionicframework 上开发应用程序。 这是我的提供程序文件(firebase.js)

commentPost(key, comment) {
    return new Promise((resolve, reject) => {
        this.dataProvider.getComments(key).take(1).subscribe((comments) => {
            var comments = comments;

            if (!comments) {
                comments = [comment];
            } else {
                comments.push(comment);
            }

            // Add both users as friends.
            this.dataProvider.postComments(key).update(comments).then((success) => {
                resolve(true)
            }).catch((error) => {
                this.loadingProvider.hide();
                reject(false)
             });
        });
    })
}

【问题讨论】:

  • {} 表示它是一个对象。 push 可用于数组

标签: typescript firebase ionic-framework ionic3 typescript2.0


【解决方案1】:

如果你想替换一个属性,你必须使用

comment['propertyName'] = newValue;

如果你想新建一个对象, 首先,您需要拥有对象数组,然后在其上使用push。 例如

array = [
{name: 'firstName', surname: 'secondName'},
{name: 'firstName1', surname: 'secondName2'}
]

然后

array.push({name: 'firstName2', surname: 'secondName2'});

【讨论】:

    猜你喜欢
    • 2016-09-01
    • 2023-04-02
    • 2018-08-17
    • 1970-01-01
    • 2023-02-06
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多