By default, NgModules are eagerly loaded. This means that as soon as the application loads, so do all the NgModules, whether they are immediately necessary or not. For large applications with lots of routes, consider lazy loading —a design pattern that loads NgModules as needed. Lazy loading helps keep initial bundle sizes smaller, which in turn helps decrease load times.
ng generate module customers --route customers --module app.module
hat is RxJS
The RxJS (Reactive Extensions Library for JavaScript) is a javascript library, that allows us to work with asynchronous data streams.
Angular uses the RxJS library heavily in its framework to implement Reactive Programming. Some of the examples where reactive programming used are:
- Reacting to an HTTP request in Angular
- Value changes / Status Changes in Angular Forms
- The Router and Forms modules use observables to listen for and respond to user-input events.
- You can define custom events that send observable output data from a child to a parent component.
- The HTTP module uses observables to handle AJAX requests and responses.
The RxJs has two main players
- Observable
- Observers ( Subscribers)
What is an Observable in Angular?
An Observable is a lazily evaluated computation that can synchronously or asynchronously return zero to (potentially) infinite values from the time it’s invoked onwards. In other words, Observable is a function that converts the ordinary stream of data into an observable stream of data.
Observable emits the value from the stream asynchronously. It emits the complete signals when the stream completes or an error signal if the stream errors out.
Observables are declarative. You define an observable function just like any other variable. The observable starts to emit values only when someone subscribes to it. Subscribing to an Observable is analogous to calling a Function.
Who are observers (subscribers)?
The Observable on its own is useless unless someone consumes the value emitted by the observable. We call them observers or subscribers.
The observers communicate with the Observable using callbacks.
The observer must subscribe with the observable to receive the value from the observable. While subscribing, it optionally passes the three callbacks. next()
, error()
& complete().
obs = new Observable((observer) => {
console.log(“Observable starts”)observer.next(“1”)
observer.next(“2”)
observer.next(“3”)
observer.next(“4”)
observer.next(“5”)})
The variable obs
is now of the type of Observable.
ngOnInit() {
this.obs.subscribe(
val => { console.log(val) }, //next callback
error => { console.log("error") }, //error callback
() => { console.log("Completed") } //complete callback
)
}
Dependency injection, or DI, is one of the fundamental concepts in Angular. DI is wired into the Angular framework and allows classes with Angular decorators, such as Components, Directives, Pipes, and Injectables, to configure dependencies that they need.
Imagine there is a class called HeroService that needs to act as a dependency in a component.
The first step is to add the @Injectable decorator to show that the class can be injected.
@Injectable()
class HeroService {}
- At the application root level, which allows injecting it into other classes in the application. This can be done by adding the
providedIn: 'root'
field to the@Injectable
decorator:
@Injectable({
providedIn: 'root'
})
class HeroService {}
When you provide the service at the root level, Angular creates a single, shared instance of the HeroService
and injects it into any class that asks for it. Registering the provider in the @Injectable
metadata also allows Angular to optimize an app by removing the service from the compiled application if it isn't used, a process known as tree-shaking.
Injecting a dependency
The most common way to inject a dependency is to declare it in a class constructor. When Angular creates a new instance of a component, directive, or pipe class, it determines which services or other dependencies that class needs by looking at the constructor parameter types. For example, if the HeroListComponent
needs the HeroService
, the constructor can look like this:
@Component({ … })
class HeroListComponent {
constructor(private service: HeroService) {}
}
When Angular discovers that a component depends on a service, it first checks if the injector has any existing instances of that service. If a requested service instance doesn't yet exist, the injector creates one using the registered provider and adds it to the injector before returning the service to Angular.
Sharing modules
Creating shared modules allows you to organize and streamline your code. You can put commonly used directives, pipes, and components into one module and then import just that module wherever you need it in other parts of your application
Comments
Post a Comment