I have created an Angular 12 application. Where I have a service FormService to retrieve information from some forms.
@Injectable({
providedIn: 'root'
})
export class FormService {
jsonUrl = './assets/forms/form.json';
defaultForm = 'form';
constructor(private http: HttpClient, private logger: LogService) {
}
getFormName(): Observable<string> {
return this.http.get(this.jsonUrl)
.pipe((res: any) => res.label);
}
...
I only show one simple method to make it simpler. I am injecting this service in the app-component ngOnInit to load some default information.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit, AfterViewInit {
constructor(
...
private formService: FormService,
...
) {
}
ngOnInit(): void {
this.formService.getFormName()
.subscribe(data => {
this.formName = data;
});
When I run the application, I get a simple error as:
this.formService.getFormName() is undefined
Then, I am trying to show the content of formService by console:
According to the structure, seems that the formService is injected, but the methods are not generated yet. In fact, formService is defined, but getFormName not!
Why the methods are not yet accessible?
