Angular - How to fix 'property does not exist on type' error?

后端 未结 5 1587
面向向阳花
面向向阳花 2020-12-29 21:42

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

error TS2339: Prop

相关标签:
5条回答
  • 2020-12-29 21:49

    If all things mentioned above checked, check

    constructor(private _employeeService: EmployeeService) { }

    private, the access specifier is there. The same issue will occur when access specifier missed.

    0 讨论(0)
  • 2020-12-29 22:01

    I would also subscribe to the service method instead of saving it to another value in ngOnInit():

    ngOnInit() {
        this.employees = this._EmployeeService.getEmployees();
    }
    

    To something like:

    ngOnInit(){
       this.getEmployees()
    }
    
    private getEmployees(): void {
        this._EmployeeService.getEmployees()
            .subscribe(fetchedEmployees = > this.employees = fetchedEmployees)
    }
    
    0 讨论(0)
  • 2020-12-29 22:03

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

    $ ng serve 
    

    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.

    0 讨论(0)
  • 2020-12-29 22:03

    Use a static keyword in the method declaration:

     public static getEmployees(): any []{
    
    }
    
    0 讨论(0)
  • 2020-12-29 22:07

    If you want to avoid the compilation warning then the dirty fix would be to make

    employees: any[];
    

    any instances allow any method to call any method on that object. This will avoid compilation warning but it's not runtime safe.

    You need to be careful before using it. If you are sure that the method will be available at runtime then only use it.

    0 讨论(0)
提交回复
热议问题