意外的标记。构造函数,方法,访问器或属性的角度预计为5

时间:2018-01-28 05:17:32

标签: javascript angular typescript

我试图将对象推入我的数组,但它显示错误

  

意外的令牌。期望构造函数,方法,访问器或属性

Model-form.component.ts文件中我创建了虚拟json数组是test。

json数组:

test: any[] = [{
        "cat_id": "1",
        "cat_name": "One",
        "cat_type": "One",
        "displayOrder": 1,
        "columns": [{
            "category": "One",
            "name": "one"
        }]
    },
    {
        "cat_id": "2",
        "cat_name": "SECURITY",
        "cat_type": "security",
        "displayOrder": 2,
        "columns": [{
            "category": "Two",
            "name": "two"
        }]
    },
    {
        "cat_id": "3",
        "cat_name": "COLLOBORATION",
        "cat_type": "colloboration",
        "displayOrder": 3,
        "columns": [{
            "category": "Three",
            "name": "three"
        }]
    },
    {
        "cat_id": "4",
        "cat_name": "NEW ARCHITECTURE",
        "cat_type": "newarch",
        "displayOrder": 4,
        "columns": [{
            "category": "Four",
            "name": "four"
        }]
    }
];

我试图将对象推入下面代码

的测试数组中
this.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});

但它显示错误。 Code URL

2 个答案:

答案 0 :(得分:1)

类型脚本不接受类中的顶级表达式。

请移动

this.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});

到函数内部并运行函数。

答案 1 :(得分:0)

看看这个:

export class ModelFormComponent implements OnInit {
 langs: string[] = [
    'English',
    'French',
    'German',
    'Chinese',
    'Vietnamese',
  ];

test: any[] = [
{
"cat_id": "1",
"cat_name": "One",
"cat_type": "One",
"displayOrder":1,
"columns": [
{
"category": "One",
"name": "one"       
}
]
},
{
"cat_id": "2",
"cat_name": "SECURITY",
"cat_type": "security",
"displayOrder":2,
"columns": [
{
"category": "Two",
"name": "two"
}
]
},
{
"cat_id": "3",
"cat_name": "COLLOBORATION",
"cat_type": "colloboration",
"displayOrder":3,
"columns": [
{
"category": "Three",
"name": "three"
}
]
},
{
"cat_id": "4",
"cat_name": "NEW ARCHITECTURE",
"cat_type": "newarch",
"displayOrder":4,
"columns": [
{
"category": "Four",
"name": "four"
}
]
}
];




this.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});

你不能在Angular 5中推送到类中的数组,类只是属性的表示

为了实现这一目标,您必须做出选择:

1):在组件范围内的方法中使用它:

ngOnInit() {
    this.myform = new FormGroup({
      name: new FormGroup({
        firstName: new FormControl('', Validators.required),
        lastName: new FormControl('', Validators.required),
      }),
      email: new FormControl('', [
        Validators.required,
        Validators.pattern("[^ @]*@[^ @]*")
      ]),
      password: new FormControl('', [
        Validators.required,
        Validators.minLength(8)
      ]),
      language: new FormControl()
    });
        this.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});

}

2):将它用作导入的类并将项目推送到数组中:

    import { Component, OnInit,NgModule, Pipe } from '@angular/core';
    import { myClass } from './models/myClass';
    import {ReactiveFormsModule,
        FormsModule,
        FormGroup,
        FormControl,
        Validators,
        FormBuilder} from '@angular/forms';

    @Component({
      selector: 'model-form',
      templateUrl: './model-form.component.html',
      styleUrls: ['./model-form.component.css']
    })
    export class ModelFormComponent implements OnInit {
   constructor(private MyClass: myClass) {
}

  ngOnInit() { 
    this.MyClass.test.push({
    'cat_name': 'fdas',
    'cat_type': 'fsda'
});
}