Angular 在数组中使用时给出错误的角度模型类

Angular 在数组中使用时给出错误的角度模型类,angular,Angular,对于这个项目,我创建了一个类,并尝试对项目中的一些内容使用构造函数格式 我的角类- import {Languages} from './temp-languages.enum'; export class Snippet { private _title: string; private _desc: string; private _code: string; private _lang: Languages; get title(): string { re

对于这个项目,我创建了一个类,并尝试对项目中的一些内容使用构造函数格式

我的角类-

import {Languages} from './temp-languages.enum';

export class Snippet {
  private _title: string;
  private _desc: string;
  private _code: string;
  private _lang: Languages;

  get title(): string {
    return this._title;
  }

  get desc(): string {
    return this._desc;
  }

  get code(): string {
    return this._code;
  }

  get lang(): Languages {
    return this._lang;
  }

  constructor(title: string, desc: string, code: string, lang: Languages) {
    this._title = title;
    this._desc = desc;
    this._code = code;
    this._lang = lang;
  }

} 
当我尝试在我的homepage-controller.ts中使用该类时,我得到一个错误,指出它应该有4个参数,但得到了7个

import { Component, OnInit } from '@angular/core';
import { Snippet } from '../models/snippet';
import { Languages } from '../models/temp-languages.enum';

@Component({
  selector: 'app-home-page-controller',
  templateUrl: './home-page-controller.component.html',
  styleUrls: ['./home-page-controller.component.scss']
})
export class HomePageControllerComponent implements OnInit {

  snippets = [
    new Snippet(title: 'My Title', desc: 'This is a short description', code: 'there is a small example here', Languages.css)
  ];

  constructor() { }

  ngOnInit() {
  }

}
我的终端也出现以下错误-

ERROR in src/app/home-page/home-page-controller/home-page-controller.component.ts(13,22): error TS1005: ',' expected.
src/app/home-page/home-page-controller/home-page-controller.component.ts(13,32): error TS1005: ',' expected.
src/app/home-page/home-page-controller/home-page-controller.component.ts(13,42): error TS1005: ',' expected.

ℹ 「wdm」: Failed to compile.

从我所看到的情况来看,一切都应该正常工作,我似乎无法理解为什么类构造函数在数组中抛出错误。非常感谢您的反馈

终端错误来自代码段构造函数中的命名参数


title:“我的标题”
更改为“我的标题”等等。

终端错误来自代码段构造函数中的命名参数


标题:“我的标题”
更改为“我的标题”等等。

实例化新对象的方式是错误的-

替换

new Snippet(title: 'My Title', desc: 'This is a short description', code: 'there is a small example here', Languages.css)


实例化新对象的方式是错误的-

替换

new Snippet(title: 'My Title', desc: 'This is a short description', code: 'there is a small example here', Languages.css)


您已经使用4个参数设置了constructu,但随后使用类似对象的结构调用了新代码段

使用以下命令:

snippets = [
    new Snippet('My Title', 'This is a short description', 
    'there is a small example here', Languages.css)
  ];

您已经使用4个参数设置了constructu,但随后使用类似对象的结构调用了新代码段

使用以下命令:

snippets = [
    new Snippet('My Title', 'This is a short description', 
    'there is a small example here', Languages.css)
  ];

尝试
新代码片段(“我的标题”,“这是一个简短的描述”,“这里有一个小例子”,Languages.css)
。感谢所有参与的人。我正在编写一个教程,他们没有从数组中删除标题、代码等。我真的很感谢大家的帮助!尝试
新代码片段(“我的标题”,“这是一个简短的描述”,“这里有一个小例子”,Languages.css)
。感谢所有参与的人。我正在编写一个教程,他们没有从数组中删除标题、代码等。我真的很感谢大家的帮助!