gpt4 book ai didi

Angular 2 : is not a function but it exist

转载 作者:太空狗 更新时间:2023-10-29 17:24:48 26 4
gpt4 key购买 nike

我试图从另一个类中获取数组,但他说该函数不存在。这是我的代码:

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

最佳答案

您需要引用参数名称,而不是参数类型

 this.courses = courseService.getCourses();
^ lower case c

关于 Angular 2 : is not a function but it exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40708944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com