I have the following array with injectables that are connected from services:
import { YouTubeSearchService,
YOUTUBE_API_KEY,
YOUTUBE_API_URL } from './you-tube.service';
export const youTubeSearchInjectables: Array<any> = [
{ provide: YouTubeSearchService, useClass: YouTubeSearchService },
{ provide: YOUTUBE_API_KEY, useClass: YOUTUBE_API_KEY },
{ provide: YOUTUBE_API_URL, useClass: YOUTUBE_API_URL }
];
And I am trying to pass it to providers in app.module.ts:
/*other imports*/
import { youTubeSearchInjectables } from './search-result/you-tube-search.injectables';
@NgModule({
declarations: [
AppComponent,
SimpleHttpComponent,
SearchResultComponent,
SearchBoxComponent
],
imports: [
BrowserModule,
FormsModule,
HttpClientModule
],
providers: [youTubeSearchInjectables],
bootstrap: [AppComponent]
})
export class AppModule { }
However, I get "ERROR in Cannot read property 'length' of undefined" (Just one line, nothing else).
I believe the reason has, most probably, something to do with version of Angular. I am using 6 and the book project is written in version 5. However, Angular documentation on Dependency Injection says that the book-way is the right way... So I am a bit confused. Or maybe I am simply missing something.
I actually tried manually adding each injectable into app.module, but got the same error.
Any help would be appreciated
EDIT: tried "providedIn: 'root'," in accordance with ng6 update, but that didn't help.
Just in case - the injectable class:
/*imports*/
export const YOUTUBE_API_KEY = 'keykeykey';
export const YOUTUBE_API_URL = 'https://www.googleapis.com/youtube/v3/search';
@Injectable({
providedIn: 'root',
})
export class YouTubeSearchService {
constructor(
private http: HttpClient,
@Inject(YOUTUBE_API_KEY) private apiKey: string,
@Inject(YOUTUBE_API_URL) private apiUrl: string
) { }
search(query: string): Observable<SearchResult[]> {
// logic
}
}