I am creating a Route Guard in Angular; how it works is depending on if you are associated with a company; it will send you to a particular component. Unfourtnetly when running canLoad method, it does not save the value of the companieslist variable. Any help would be appreciated!
import { Injectable } from '@angular/core';
import { CanActivate, Router, CanLoad } from '@angular/router';
import { AuthService } from 'src/app/auth.service';
import { User } from 'src/models/user';
import {ListdataService} from 'src/app/listdata.service';
@Injectable({
providedIn: 'root'
})
export class CompanyonlyService implements CanActivate, CanLoad {
companieslist=[];
constructor(private authService: AuthService, private router: Router,private dataService:ListdataService) { }
run(){
this.dataService.sendGetRequestcompanies().subscribe((data: any[])=>{
let tmp = [];
for (let key in data)
if (data.hasOwnProperty(key))
tmp.push(data[key])
this.companieslist = tmp;
console.log(this.companieslist)
} )}
canActivate() {
return this.canLoad()
}
//this method won't set the value of the variable why?
canLoad(){
this.run()
console.log('after loop')
console.log(this.companieslist)
if (this.companieslist.length==0) {
this.router.navigate(['/companysignup']);
}
else{
this.router.navigate(['/dashboard']);
}
return this.authService.isLoggedIn();
}
}
[enter image description here]this is what I am returning in the console