Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

    ERROR Error: StaticInjectorError[e -> t]: 
  StaticInjectorError(Platform: core)[e -> t]: 
    NullInjectorError: No provider for t!
    at e.get (core.js.pre-build-optimizer.js:8894)
    at core.js.pre-build-optimizer.js:9139
    at e (core.js.pre-build-optimizer.js:9083)
    at e.get (core.js.pre-build-optimizer.js:8980)
    at core.js.pre-build-optimizer.js:9139
    at e (core.js.pre-build-optimizer.js:9083)
    at e.get (core.js.pre-build-optimizer.js:8980)
    at Ua (core.js.pre-build-optimizer.js:21119)
    at e.get (core.js.pre-build-optimizer.js:21808)
    at Ts (core.js.pre-build-optimizer.js:22179)

But it working fine with Ionic Cordova build android also it working with ng serve; but I am getting the same above error with ng serve --prod".

How can I resolve this?

like image 555
sahil tuteja Avatar asked Sep 15 '25 19:09

sahil tuteja


2 Answers

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

like image 89
TheBosti Avatar answered Sep 18 '25 10:09

TheBosti


For anyone who could need this:

The reasons stated above are one case scenario where StaticInjectorError could be thrown. However, it's not the only situation. In other cases, it could happen because you are trying to inject a dependency that is undefined. That is: a) "Dumb case":You haven't imported the service, but you injected anyway. Mosts IDE's will catch that for you b) Complicated case: say you have custom libraries that depends ones from another and you don't set the right peer dependencies in each of them. This will cause inconsistency between modules (different version of the same library in your app). On top of that, if you use Ahead-of-time compilation, it might happen that the service you try to import is not reached from the proper module (library version). This will cause that the service = undefined, hence, it would be like case a)

Talking from my own experience!

like image 35
Kenny Martin Rguez Avatar answered Sep 18 '25 09:09

Kenny Martin Rguez