I'm not sure if I'm doing the right thing here. I need to create a base class, so that my controls can inherit from it. So I did this base class:
import { Location } from '@angular/common';
export class FormBaseComponent {
constructor(
protected location: Location
) {}
goBack() {
alert('back');
this.location.back();
}
}
and a class that inherit from it:
import { Component, OnInit } from '@angular/core';
import {
FormBuilder
} from '@angular/forms';
import { UserService } from 'app/shared/services/user/user.service';
import { User } from 'app/models/authentication/user';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { FormBaseComponent } from 'app/shared/components/form-base.component';
@Component({
selector: 'app-my-account',
templateUrl: './my-account.component.html',
styleUrls: ['./my-account.component.scss']
})
export class MyAccountComponent extends FormBaseComponent implements OnInit {
formEdit = this.fb.group(User.formGroup());
constructor(
private fb: FormBuilder,
private userServices: UserService,
private router: Router,
location: Location
) {
super(location);
}
ngOnInit() {
this.userServices.getCurrentUser().subscribe( x => {
this.formEdit.patchValue(x);
});
}
onSubmit() {
this.userServices.update(this.formEdit.getRawValue()).subscribe( x => {
this.router.navigate(['/']);
});
}
}
Is it correctly to declare this "location" parameter in the constructor like this?
location: Location
) {
super(location);
}
BTW, this code works as expected.