When trying to call a component method from a service class, an error is encountered: 'ERROR TypeError: Cannot read property 'test' of undefined'. Although similar issues have been researched, the explanations mostly focus on component-to-component calling, leaving confusion in understanding.
Here is an example:
Testcomponent.ts@Component({
selector:'component'
})
export class Testcomponent{
test(){
console.log('test method');
}
}
@Injectable()
export class Testservice {
private testcomp: Testcomponent;
// service method
dummy(){
//trying to call component method
testcomp.test();
}
}
This approach of calling the component method raises doubts about its correctness. Assistance is needed to understand the proper way to achieve this task.
Further research on Stack Overflow did not provide a clear understanding: How to call component method from service? (angular2)