I want to inject a service into a class that is not a component.
For example:
Myservice
import {Injectable} from '@angular/core';
@Injectable()
export class myService {
dosomething() {
// implementation
}
}
MyClass
import { myService } from './myService'
export class MyClass {
constructor(private myservice:myService) {
}
test() {
this.myservice.dosomething();
}
}
This solution doesn't work (I think because MyClass
hasn't been instantiated yet).
Is there another way to use a service in a class (not component)? Or would you consider my code design as inappropriate (to use a service in a class which is not a component)?
Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…