[英]Angular2: Cannot find a differ supporting object from service class
I am following a course on Udemy-Angular 2 with TypeScript for Beginners and ran into an error that is not that obvious to me wrt Services.我正在学习有关Udemy-Angular 2 和 TypeScript for Beginners的课程,但遇到了一个对我来说并不明显的错误服务。
I have a component hat is displaying a list of courses.我有一个组件帽子正在显示课程列表。 Or a list of string arrays.
或者一个字符串数组列表。
As the course implies that in real life you will not get a hard coded list of items to display but will call some service in order to retrieve it.正如课程所暗示的那样,在现实生活中,您不会获得要显示的项目的硬编码列表,而是会调用一些服务来检索它。 The example calls another service class to retrieve it (for now in this class it is a hard coded list that it returns)
该示例调用另一个服务类来检索它(现在在这个类中,它返回的是一个硬编码列表)
I have a service class called CourseService that returns an array of strings:我有一个名为 CourseService 的服务类,它返回一个字符串数组:
export class CourseService {
getCourses() : string[] {
return ["Course1", "Course2", "Course3"];
}
}
and my component has a template using an *ngFor to loop through a local variable called courses.并且我的组件有一个模板,使用 *ngFor 来循环调用名为courses的局部变量。 The courses variable gets instantiated by fetching the array from the service class using dependency injection in the constructor:
通过在构造函数中使用依赖注入从服务类获取数组来实例化课程变量:
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: string = "The titles of courses page"
courses;
constructor(courseService:CourseService) {
//if i get the courses array from the courseService I get the following error in the browser
/*angular2.dev.js:23083 EXCEPTION: Cannot find a differ supporting object 'function () {
return ["Course1", "Course2", "Course3"];
}' in [courses in CoursesComponent@4:16]
I cannot see what is wrong at line 4 in CoursesComponent as it is returning the same string*/
this.courses = courseService.getCourses;
//If I comment out the above line of codeand don't fetch the courses from the service but from a new array it works.
//this.courses = ["Course1", "Course2", "Course3"];
}
}
When I run this example the courses do not display and I get the following error in the console:当我运行此示例时,课程不会显示,并且在控制台中出现以下错误:
The class is simply returning a string array.该类只是返回一个字符串数组。 If I swop out that fetch with a hardcoded string[] all works and display correctly.
如果我用硬编码的 string[] 替换掉那个 fetch,一切正常并正确显示。
Not sure if there is a problem with the actual class or the dependency injection.不确定是实际类还是依赖注入有问题。 I suppose since it is complaining about the class and line 4 it does actually instantiate and uses the class.
我想因为它在抱怨这个类和第 4 行,所以它确实实例化并使用了这个类。
As per @5313M suggestion using the this is resulting in the following:根据@5313M 的建议,使用 this 会导致以下结果:
However I see that as I had my code I am getting the following error: The function is returning string[] and my variable is string[].但是我看到,当我有我的代码时,我收到以下错误:函数正在返回 string[],我的变量是 string[]。 Why will this conflict:
为什么会这样冲突:
将其更改为:
this.courses = courseService.getCourses();
Please replace your code with the below and try:请用以下代码替换您的代码并尝试:
<li *ngFor='let course of courses'>
{{ course }}
</li>
courses : string[];
this.courses = this.courseService.getCourses();
Very silly issue.很傻的问题。 Will teach me not to use intellisense/autocomplete.
会教我不要使用智能感知/自动完成。
As per the course recommendation we should use vs editor.根据课程推荐,我们应该使用 vs 编辑器。 Not sure if it is the editor or angular-2 but if you go courseService.
不确定它是编辑器还是 angular-2 但如果你去 courseService。 and Ctrl+Space for it to complete and select the method it completes the method but doesn't add the () on the method calls.
和 Ctrl+Space 让它完成并选择它完成方法但不添加 () 的方法调用的方法。
Hence I had:因此我有:
this.courses = courseService.getCourses;
Instead of:代替:
this.courses = courseService.getCourses();
this.courses = courseService.getCourses;
您没有在方法的调用中放置括号:
this.courses = courseService.getCourses();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.