I'm facing an issue with defining a redirect from my login to the dashboard after successfully verifying a user. When I try to implement "this.router.navigate(['/dashboard'])" in my login component, I encounter the following error: "exception: undefined is not an object (evaluating 'this.router')"
Below is how I structured the function:
export class ContactComponent implements OnInit {
contactForm: FormGroup;
errorMsg:string = '';
constructor(private formBuilder: FormBuilder, private _router: Router) {
this._router = _router;
}
ngOnInit() {
this.contactForm = this.formBuilder.group({
username: ['', Validators.required],
password: ['', Validators.required],
});
DBEventProxy.instance(); // Init Eventbus
}
submitForm(): void {
DBEventProxy.instance().dbevent.login(this.contactForm['username'], this.contactForm['password'], this.loggedIn, this.failed);
}
loggedIn(): void {
this._router.navigate(['/dashboard']);
console.log("success");
DBEventProxy.instance().dbevent.register("disconnected", function (message) {
console.log("Client disconnected: " + JSON.stringify(message));
});
DBEventProxy.instance().dbevent.register("status", function (message) {
console.log("Client reconnected: " + JSON.stringify(message));
});
DBEventProxy.instance().dbevent.register("routeravailable", function (message) {
console.log("Router Available: " + JSON.stringify(message));
});
DBEventProxy.instance().dbevent.register("routerunavailable", function (message) {
console.log("Router Unavailable: " + JSON.stringify(message));
});
DBEventProxy.instance().dbevent.listen();
DBEventProxy.instance().dbevent.send({msgtype: "dashboardready"});
}
failed(message: string): void {
console.log("failed: " + message);
}
}
I would greatly appreciate any assistance with this issue!
Update: I am encountering errors when attempting to log in using my credentials. The ConnectReply from the Server is received and the connection exists, but the redirect fails to work. Here are the specific errors I have identified:
- EXCEPTION: undefined is not an object (evaluating 'this._router')
- ORIGINAL STACKTRACE: (multiple documents and handleErrors included)
- TypeError: undefined is not an object (evaluating 'this._router')
Routes configuration:
export const rootRouterConfig: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'contact', component: ContactComponent },
{ path: '', redirectTo: '/contact', pathMatch: 'full'}
];