Я знаком с Resolves, но не думаю, что это мне поможет.
Я ищу простой способ сначала загрузить app.component, даже если для URL задан дочерний компонент. (Например, после обновления).
Или способ ожидания выполнения моей глобальной переменной currentUid
(объект, содержащий соответствующие данные.
Или глобальная переменная в службе, к которой я смогу получить доступ.
Но будет замечательно, если app.component будет загружаться первым (как и любимый угловой JS).
Большое спасибо!
app.component.ts:
export let currentUid;
getCurrentUser(uid) {
this.login = false;
this.getService.getCurrentUserService(uid).subscribe(response => {
if (response[0] === undefined) {
this.login = false;
console.log('Coach already register? ', this.login);
$('#errorUserModal').modal('show');
$('#registerFirst').css('display', 'block');
setTimeout(function() { $('#errorUserModal').modal('hide'); $('#registerFirst').css('display', 'none'); }, 4000);
this.getService.shareUser(uid);
this.router.navigateByUrl('/register');
} else {
this.login = true;
console.log('Coach already register? ', this.login);
this.setUser(response[0]);
this.getService.shareUser(response[0]);
this.router.navigateByUrl('/your-clients');
}
},
error => {
console.log('Problem with getting current user ', error);
});
}
setUser(coach) {
this.login = true;
this.userNameOnMenu = coach.name;
this.userImageOnMenu = coach.logo;
userName = coach.name;
userImage =coach.logo;
currentUid = coach;
this.userImagePath = uploadedFolderPath + this.userImageOnMenu;
}
your-clients.components.ts:
import { Component, OnInit, AfterContentInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
import { GetService } from '../get.service';
import { currentUid } from '../app.component';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/combineLatest';
declare var jQuery: any;
declare var $: any;
@Component({
selector: 'app-your-clients',
templateUrl: './your-clients.component.html',
styleUrls: ['./your-clients.component.css']
})
export class YourClientsComponent implements OnInit, AfterContentInit {
clients;
constructor(public router: Router, public getService: GetService, public route: ActivatedRoute) { }
ngOnInit() {
}
ngAfterContentInit() {
// console.log(currentUid) print undefined
// Option #2:
this.getService.observable.subscribe(response => {
// How to turn response accessible in all class?
// something like: alternativeUid = response
console.log(response);
});
// this.getCurrentClients(alternativeUid.uid);
}
getCurrentClients(param) {
this.getService.getCurrentClientsService(param).subscribe(response => {
console.log(response);
this.clients = response;
},
error => {
console.log('Problem with getting current clients ', error);
});
}
}
get.service.ts:
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class GetService {
obj;
shareCoachBetweenComponentsFunction: Subject<any> = new Subject<any>();
observable: Observable<any> = this.shareCoachBetweenComponentsFunction.asObservable();
constructor(public http: HttpClient, public router: Router) { }
shareUser(argument) {
this.shareCoachBetweenComponentsFunction.next(argument);
}
getCurrentUserService(user) {
const nodeFunctionUrl = '/university/getCurrentUser';
const param = user;
return this.http.get(url + nodeFunctionUrl + '/?data=' + param);
// .map((response: Response) => {
// const coachName = response[0].name;
// const coachImage = response[0].logo;
// return response;
// });
}
getCurrentClientsService(uid) {
const nodeFunctionUrl = '/university/getCurrentClients';
const param = uid;
return this.http.get(url + nodeFunctionUrl + '/?data=' + param);
}