Introduction to services and dependency injection

Introduction to services and dependency injection

service is a code that are used to perform a specific task, a service can contain a value or function or combination of both.

Service is injected into the application using dependency injection mechanism.

Service provides its data and methods other needs components.
How to create a service:- you can easily generate service. Open your terminal and type:


ng generate service auth

Import the Injectable Member:-At the top of your new service file, add:

import { Injectable }from'@angular/core';

Include it through dependency injection:- In the constructor argument of the component class, we include it through dependency injection:

constructor(
private afAuth: AngularFireAuth,
private router: Router,
private afs: AngularFirestore
) {}

Create a Method and use another component:-So, guys, you can finally create the method and use need components.

So let’s go and see the little example.

auth.service.ts

emailSignUp(email: string, password: string) {
return this.afAuth.auth.createUserWithEmailAndPassword(email, password);
}

signUp.component.ts

onSubmit(model, f) {
this.auth.emailSignUp(model.email, model.password).then(res => {
this.snackBar.open('sign up success', '', { duration: 3000 });
f.reset();
}).catch(err => {
this.snackBar.open(err, '', { duration: 3000 });
});
}

                      -:Thanks for the reading:-
Previous
Next Post »

1 comments:

Click here for comments
M. Taha
admin
May 28, 2020 at 2:32 AM ×

I learn some new stuff from it too, thanks for sharing your information. 2k moulding

Congrats bro M. Taha you got PERTAMAX...! hehehehe...
Reply
avatar