Reputation: 157
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!
Upvotes: 0
Views: 171
Reputation: 364
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)
];
Upvotes: 2
Reputation: 11243
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)
Upvotes: 2
Reputation: 143
The Terminal error is from the named parameters in the Snippet constructor.
Change title: 'My Title'
to just 'My Title'
and so forth.
Upvotes: 2