Angular Error : StaticInjectorError (Platform: core)[e -> t]:

后端 未结 2 601
有刺的猬
有刺的猬 2021-01-20 17:18

As I am build the APK with --prod I am getting the error below

    ERROR Error: StaticInjectorError[e -> t]: 
  StaticInjectorError(Platform:         


        
相关标签:
2条回答
  • 2021-01-20 18:02

    You are trying to use a service that is not listed in providers of your AppModule or inside you component.ts. Add the service to a providers list to make it work.

    In app.modules if you want that service to be global (related to app context).

    @NgModule({
        declarations: [...],
        imports: [...],
        bootstrap: [...],
        entryComponents: [...],
        providers: [
            MyService
        ]
    })
    

    Or in your component.ts if you want that service to be contextual to desired component.

    @Component({
        selector: '...',
        templateUrl: '...',
        providers: [MyService]
    })
    

    Do not add it in both files. Also don't forget to import that service when you inject it either in app.modules or component.

    import { MyService } from '../services/myservice';
    

    This question also may help you: Error: No provider for t

    0 讨论(0)
  • 2021-01-20 18:12

    This happens because you did not add the services used to build the module

    example:

    1. component

    @Component({
      selector: 'app-sample',
      templateUrl: './sample.component.html',
      styleUrls: ['./sample.component.scss']
    })
    export class SampleComponent implements OnInit {
    
    constructor(private localServiceName: YourService) {
    }
    
      ngOnInit() {
      }
    
    }
    

    2. module

    @NgModule({
        declarations: [SampleComponent]
    })
    

    change to

    @NgModule({
        declarations: [SampleComponent]
        providers: [ YourService ]
    })
    
    0 讨论(0)
提交回复
热议问题