我熟悉Resolves,但我认为它不能帮助我。
我正在寻找一种简单的方法来加载app.component,即使URL设置为子组件。 (例如刷新后)。
或者,等待我的全局var currentUid
实现的方法(包含相关数据的obj。
或者我将能够访问的服务中的全局变量。
但是如果首先加载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);
}