【发布时间】:2018-09-27 19:14:39
【问题描述】:
我正在关注this video tutorial (text version of the same)。我遵循了完全相同的代码,我收到了这个错误:
错误 TS2339:类型上不存在属性“getEmployees” '员工服务'
我在 Internet 上查看并访问了 Stack Overflow 上的许多问题,例如 this、this、this 和 this,以及在 GitHub 上打开了与此错误相关的许多其他问题。
服务:
//import statements go here ...
@Injectable()
export class EmployeeService {
private listEmployees: Employee[] = [
{
//just to avoid longer code, deleted dummy data.
},
];
getEmployees(): Employee[] {
return this.listEmployees; //ERROR in src/app/employees/list-employees.component.ts(14,44)
}
}
组件类:
//import statements
@Component({
selector: 'app-list-employees',
templateUrl: './list-employees.component.html',
styleUrls: ['./list-employees.component.css']
})
export class ListEmployeesComponent implements OnInit {
employees: Employee[];
constructor(private _EmployeeService: EmployeeService) { }
ngOnInit() {
this.employees = this._EmployeeService.getEmployees();
}
}
我已在app.module.ts 中导入服务并将其添加到ngModule 的提供者数组中。
我无法解决错误,也无法理解导致此错误的原因。
【问题讨论】:
-
你能放 plunker 或 stackblitz 吗?这将有助于更好地调试您的代码
-
确切的错误是什么?你能发布完整的错误吗?不要只是在代码中添加注释,将人们指向该行并期望其他人知道错误到底是什么。
-
我已经提到了问题本身的错误。
-
这种行为也发生在 ionic 4 CLI 中。因此,如果缺少服务,它的成员会重新启动
ionic serve会有所帮助。
标签: angular typescript