简体   繁体   English

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

[英]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'
});

but it's showing error. 但显示错误。 the Code URL 代码网址

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.

相关问题 Angular :意外的令牌。 需要构造函数、方法、访问器或属性 - Angular : unexpected token. A constructor, method, accessor, or property was expected 问:错误:意外的令牌。 需要构造函数、方法、访问器或属性。ts(1068) - Q: Error: Unexpected token. A constructor, method, accessor, or property was expected.ts(1068) TypeScript 意外标记,需要构造函数、方法、访问器或属性 - TypeScript Unexpected token, A constructor, method, accessor or property was expected TypeScript:意外的令牌; '构造函数,函数,访问器或变量' - TypeScript : Unexpected token; 'constructor, function, accessor or variable' reactJS 意外令牌,应为“;” 在构造函数中 - reactJS Unexpected token, expected “;” in constructor TypeScript- NodeJS-意外令牌; “构造函数,函数,访问器或变量” - TypeScript- NodeJS - Unexpected token; 'constructor, function, accessor or variable' React Native 构造函数:意外的标记,预期的“;” - React Native constructor: Unexpected token, expected “;” 语法错误:意外的标记。 异步/等待节点 8 - SyntaxError: Unexpected token. async/await node 8 一个新对象抛出“意外令牌”。 - a new object throw “Unexpected token.” nodejs React组件构造函数(解析错误:意外令牌,期望“}”) - React component constructor (Parsing error: Unexpected token , expected “}”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM