0

For this project, I have created a class and I am trying to use the constructor format for some of the content on the project.

My Angular class -

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;
  }

} 

When I attempt to use the class in my homepage-controller.ts I am getting an error stating it expected 4 arguments, but it got 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() {
  }

}

I am also getting the following error in my terminal-

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.

From what I can see everything should be working fine and I can't seem to understand why the class constructor is throwing an error in the array. Any feedback is most appreciated!

2
  • Try new Snippet('My Title', 'This is a short description', 'there is a small example here', Languages.css). Commented Oct 10, 2018 at 20:37
  • Thank you to everyone who jumped in. I'm working on a tutorial and they didn't remove the title, code, etc from the array. I really appreciate everyone's help! Commented Oct 10, 2018 at 20:40

3 Answers 3

2

The Terminal error is from the named parameters in the Snippet constructor.

Change title: 'My Title' to just 'My Title' and so forth.

Sign up to request clarification or add additional context in comments.

Comments

2

The way you are instantiating new object is wrong -

Replace

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

by

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

Comments

2

You have setup the constructu with 4 arguments but then you call the new Snippet with a object like structure.

Use the following:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.