File 'app/hero.ts' is not a module error in the console, where to store interfaces files in directory structure with angular2?

后端 未结 24 1835
无人共我
无人共我 2020-12-13 07:55

I am doing the angular2 tutorial at this address: https://angular.io/docs/ts/latest/tutorial/toh-pt3.html I have put the hero interface in a single

相关标签:
24条回答
  • 2020-12-13 08:25

    Try Restarting the editor in which you are writing the code(VS code or Sublime). Compile and Run it again. I have done the same and it worked.

    This happens when you add a new class outside from your editor or keep running your angular cli 'ng serve'. Actually your editor or the 'ng serve' command may not able to find the newly created files.

    0 讨论(0)
  • 2020-12-13 08:25

    I got the same error in the same tutorial because I had forgot the export keyword for the interface.

    0 讨论(0)
  • 2020-12-13 08:25

    The tutorial has you putting all components and the interface file in the same directory hence the relative import './hero' if you want to move it elsewhere you need to change this.

    Edit: The code may still work but the compiler will complain because you are attempting an import from an incorrect location. I'd simply change the import based on your structure. For example:

    app/
      component1/
        component1.ts
      compnent2/
        component2.ts
      hero.ts
    

    I would simply change the import to import {Hero} from '../hero'.

    0 讨论(0)
  • 2020-12-13 08:25

    change

    import{observable} from 'rxjs/observable'; 
    

    to

    import{observable} from 'rxjs';
    

    make sure you have save .TS file before compiling.

    0 讨论(0)
  • 2020-12-13 08:28

    For people getting the same error on stackblitz

    You will find a Dependency tab on the left sidebar of the IDE. Just click the refresh button next to it and you will be good to go.


    0 讨论(0)
  • 2020-12-13 08:28

    Don't forget to export your class or interface as well.

    export interface Sort {
        value: string;
        viewValue: string;
    }
    
    0 讨论(0)
提交回复
热议问题