6

I am trying to get an array out of a different class but he says the function does not exist. here is my code:

courses.component.ts:

import {Component} from 'angular2/core'
import {CourseService} from './course.service'

@Component({
    selector: 'courses',
    template: `
        <h2>Courses</h2>
        {{ title }}
        <ul>
            <li *ngFor ="#course of courses">
            {{course}}
            </li>
        </ul>
        `,
    providers: [CourseService]
})
export class CoursesComponent{
    title = "The title of courses page";
    courses;

    constructor(courseService: CourseService){
        this.courses = CourseService.getCourses();
    }
}

course.service.ts:

export class CourseService{
    getCourses() : string[]{
        return ["Course1","Course2","Course3"];
    }
}
2
  • Yup, that probably as well, depends on what he tries to accomplish. Commented Nov 20, 2016 at 20:25
  • restarting the dev server fixed it for me, inexplicable!! Commented Sep 17, 2018 at 14:19

3 Answers 3

7

You need to reference the argument name, not the argument type

 this.courses = courseService.getCourses();
                ^ lower case c
Sign up to request clarification or add additional context in comments.

6 Comments

It still gives the same error: TypeError: courseService.getCourses is not a function
where should I past that?
first line inside the constructor (before the line from my answer)
Change ; to : in ` getCourses() ; string[]{. Also you should get an output from the console.log(courseService);` if you have added it in the constructor before this.courses = courseService.getCourses();.
I edited the question assuming that was a mistake, shout have noted it, ^
|
2

I think it's some kind of bug, because TypeScript recognize me the method but when I delete and type the method again getCourses() in the component, it says that the method is not found, then I go to the Service and start to delete blank lines, and the method Works. I'm currently using Angular 4

Comments

1

I had the same inexplicable problem. I copy and pasted the code from the web and something must not have been right in the function declaration. I deleted the line

getCourses(): string[] {

and typed it out again by hand. When I ran the code, it worked. Maybe some invisible character was pasted in and messing up Typescript? Who knows.

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.