[英]Unexpected token. A constructor, method, accessor, or property was expected angular 5
I tried to push object into my array, but it showing the error 我试图将对象推入数组,但显示错误
Unexpected token.
意外的标记。 A constructor, method, accessor, or property was expected
预期构造函数,方法,访问器或属性
In Model-form.component.ts
file i have created dummy json array is test. 在
Model-form.component.ts
文件中,我创建了虚拟json数组进行了测试。
json array: 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"
}]
}
];
and i have tried to push object into the test array given below code 我试图将对象推入下面的代码给出的测试数组中
this.test.push({
'cat_name': 'fdas',
'cat_type': 'fsda'
});
Type script do not accept top level expressions inside a class. 类型脚本不接受类内的顶级表达式。
Please move 请移动
this.test.push({
'cat_name': 'fdas',
'cat_type': 'fsda'
});
to inside a function and run the function. 到函数内部并运行该函数。
look at this: 看这个:
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'
});
You cannot push to an array inside of class in Angular 5, a class is merely a representation of properties . 您不能在Angular 5的类内部推送到数组,类仅是properties的表示 。
In order for this to work you have to choices: 为了使它起作用,您必须选择:
1): Use it inside a method in your component scope: 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): Use it as an imported class and push the items into the array: 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'
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.