网站搜索

Visual Studio - TS 属性 Push 在 Angular 代码 VS 中的类型 '{}' 上不存在


我创建了一个数组

    messages: string[] =[];

之后我尝试去 push 一个 string 值

    add(message: string){
       this.messages.push(message);
    }

在 Visual Studio 中我有错误:

 [ts] Property 'push' does not exist on type '{}'.

答案#1

你可以尝试这个 messages: string[]=new Array<string>();

答案#2

试试我在项目中用的这个,它有效

public roles = new Array<any>();

并将你的枚举对象推入数组

for (let item in Role)

{
      if (item)
          {
                let temp=item;
                this.roles.push(temp);
               }
}

需要检查 console.log(this.roles);