I have added multiple components in my Angular app including a registration component which allows users to register themselves; there was no issue with the frontend part until I added the methods for register.ts & userservice.ts file. After adding the methods & services, the registration page does not load when I run the app.
My question is that if any of the methods & services might be interfering with routing of the component?
These are my .ts files:
register.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { UserService } from 'src/app/register/userservice'; //import the user service
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; //import form modules
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.less']
})
export class RegisterComponent implements OnInit {
registrationForm: FormGroup;
email:string ='';
password:string ='';
submitted = false;
constructor(
private formBuilder: FormBuilder,
//private router: Router,
private UserService: UserService,
)
{
this.registrationForm = this.formBuilder.group({});
}
ngOnInit() {
this.registrationForm = this.formBuilder.group({
email: [this.email, [Validators.required, Validators.email]],
password: [this.password, [Validators.required, Validators.minLength(6)]],
});
}
// convenience getter for easy access to form fields
get f() { return this.registrationForm.controls; }
onSubmit() {
this.submitted = true;
if (this.registrationForm.invalid) {
return;
}
//call the user service to register the user
this.UserService.register(this.registrationForm.controls['email'].value, this.registrationForm.controls['password'].value)
.subscribe(data => {
// handle the response
});
//error handling can b introduced
}
}
userservice.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class UserService {
private baseUrl = 'http://localhost:8080/'; // replace with your backend URL
constructor(private http: HttpClient) { }
//check the use of 'Observable'
register(email: string, password: string): Observable<any> {
const data = {
email: email,
password: password
};
return this.http.post(`${this.baseUrl}/register`, data);
}
}