角度-如何解决“类型上不存在属性"错误? [英] Angular - How to fix 'property does not exist on type' error?

查看:76
本文介绍了角度-如何解决“类型上不存在属性"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注此视频教程(同一文本的文本).我遵循了完全相同的代码,并且收到此错误:

I am following this video tutorial (text version of the same). I have followed exactly the same code and I am receiving this error:

错误TS2339:类型上不存在属性"getEmployees"'EmployeeService'

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

我在Internet上浏览了许多有关堆栈溢出的问题,例如 this 这个,还有很多其他与此GitHub上的错误相关的问题.

I looked on the Internet and visited many questions on Stack Overflow like this, this, this and this, and so many others issues opened related to this error on 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 的provider数组中.

I have imported service in app.module.ts and added it in providers array of ngModule.

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

I am not able to solve the error neither to understand what is causing this error.

推荐答案

通常在开发Angular应用程序时发生.要解决此问题,只需关闭服务器和服务器即可.重新开始:

It usually happens when you develop Angular applications. To solve this just shut down the server & start it again:

$ ng serve 

说明

这是因为启动应用程序时,服务器实际上正在提供存储在dist文件夹中的分发包(JavaScript/CSS/HTML ...输出文件).有时,当您在代码中进行更改时,所做的更改不会反映在您的捆绑软件中,这将导致服务器仍在使用旧的捆绑软件.

Explanation

This happens because when you start the application, The server is actually serving the bundles(JavaScript/CSS/HTML... output files) stored in the dist folder. Sometimes, when you make changes in your code, the changes don't reflect in your bundles, which will lead to the server still using the old bundles.

这篇关于角度-如何解决“类型上不存在属性"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆