Reputation: 1869
Hello im trying to implement firestore into my web application, when i add the service to contructor the error:
NullInjectorError: No provider for TesteventService!
I'm using Angular 5, angularfire2/firestore and typescript 2.7.1
testevent.service.ts
import { Injectable } from '@angular/core';
import { AngularFirestore } from 'angularfire2/firestore'
@Injectable()
export class TesteventService {
constructor(private afs: AngularFirestore) { }
addEvent(eventData){
this.afs.collection('testevent').add(eventData).then(() => {
console.log('Done');
})
}
getEvent(){
return this.afs.collection('testevent', ref => ref.orderBy('id')).valueChanges()
}
}
component.ts
import { Component, OnInit } from '@angular/core';
import { TesteventService } from '../../providers/testevent.service';
import { AngularFirestore } from 'angularfire2/firestore';
export class CsvImportComponent implements OnInit {
data_rows = []
constructor(private event: TesteventService){})
When I add events from TesteventSerivice i get the error, while nothing is run.
Upvotes: 58
Views: 143753
Reputation: 2430
There are two possibilities to solve this problem:
imports: [ ], declarations: [ ], providers: [ TesteventService ]
@Injectable({ providedIn: 'root' })
Upvotes: 16
Reputation: 1994
Here is how i have solved my problem with how to pass runtime parameter to a angular service which is part of another custom library
It was difficult to identify where and how to provide injected parameter
Here my AuthService is part of another library i used providedIn: 'root' because my service requirement was to define it as a singleton service. And there are many ways to achieve that ...
@Injectable({
providedIn: 'root',
})
export class AuthService {
constructor(
@Inject('defaultUserName') private defaultUserName: string,
@Inject('defaultPassword') private defaultPassword: string
) {
// Now you can use injected parameters
console.log("defaultUserName", defaultUserName);
console.log("defaultPassword", defaultPassword);
}
// More code and methods
}
Here is how i use above service in my project
import { CoreApiModule } from 'my-library/app';
import { **AuthService** } from 'my-library/app/api/auth.service';
@NgModule({
imports: [
CoreApiModule
],
declarations: [
],
bootstrap: [AppComponent],
providers: [
AuthService,
{
provide: 'defaultUserName',
useValue: Constants.userName,
},
{
provide: 'defaultPassword',
useValue: Constants.defaultPassword,
}
]
})
export class AppModule {}
Now you can directly import AuthService in any component and start using it.
Upvotes: 3
Reputation: 222722
You need to add TesteventService under providers under imports in your app.module.ts
providers: [
TesteventService
]
Upvotes: 109