gpt4 book ai didi

Angular - 如何修复 'property does not exist on type' 错误?

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

我正在关注 this video tutorial (text version of the same)。我遵循了完全相同的代码,但收到此错误:

error TS2339: Property 'getEmployees' does not exist on type 'EmployeeService'

我在 Internet 上查看并访问了 Stack Overflow 上的许多问题,例如 this , this , thisthis ,以及 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 的 providers 数组中。

我无法解决错误,也无法理解导致此错误的原因。

最佳答案

它通常发生在您开发 Angular 应用程序时。要解决这个问题,只需关闭服务器并重新启动它:

$ ng serve 

说明

发生这种情况是因为当您启动应用程序时,服务器实际上正在为存储在 dist 文件夹中的包(JavaScript/CSS/HTML...输出文件)提供服务。有时,当您对代码进行更改时,更改不会反射(reflect)在您的包中,这将导致服务器仍在使用旧包。

关于Angular - 如何修复 'property does not exist on type' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49884563/

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