Reputation: 183
I have created module in Angular 4. Also i created service using @Injectable, added to module. Now my requirement is , service should not load during module loading. once i perform action or event like click, then service should load. So please help me how to implement this requirement.
thanks, Narsi p
Upvotes: 4
Views: 1084
Reputation: 352
If you are using Angular 6 then there is a feature provided by angular itself to lazy load services by using it's new syntax.
@Injectable({
providdedIn : 'root'
})
Using this way Service can be loaded lazy by angular behind the scene, which will remove redundant code and lead to better performance and speed.
Upvotes: 1
Reputation: 272
You have to use injector explicitly. The injector is the one which instantiate the service.
Suppose square is the service,
class Square { name = 'square'; }
Call the service in component using below code.
const injector = Injector.create({providers: [{provide: Square, deps: []}]});
const shape: Square = injector.get(Square);
expect(shape.name).toEqual('square');
expect(shape instanceof Square).toBe(true);
Upvotes: 1