forked from damoqiongqiu/NiceFish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.component.ts
More file actions
85 lines (72 loc) · 2.65 KB
/
Copy pathapp.component.ts
File metadata and controls
85 lines (72 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Component, HostListener, ElementRef, Renderer, ViewContainerRef } from '@angular/core';
import { ActivatedRoute, Router, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot } from '@angular/router';
import { TranslateService } from 'ng2-translate';
import { UserLoginService } from './user/user-login/user-login.service';
import { UserRegisterService } from './user/user-register/user-register.service';
import { User } from './user/model/user-model';
import 'rxjs/add/operator/merge';
import { Message } from 'primeng/primeng';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
public msgs: Message[] = [];
public currentUser: User;
private globalClickCallbackFn: Function;
private loginSuccessCallbackFn: Function;
constructor(
public elementRef: ElementRef,
public renderer: Renderer,
public router: Router,
public activatedRoute: ActivatedRoute,
public translate: TranslateService,
public userLoginService: UserLoginService,
public userRegisterService: UserRegisterService
) {
}
ngOnInit() {
this.globalClickCallbackFn = this.renderer.listen(this.elementRef.nativeElement, 'click', (event: any) => {
console.log("全局监听点击事件>" + event);
});
this.currentUser = JSON.parse(localStorage.getItem("currentUser"));
this.userLoginService.currentUser
.merge(this.userRegisterService.currentUser)
.subscribe(
data => {
this.currentUser = data;
let activatedRouteSnapshot: ActivatedRouteSnapshot = this.activatedRoute.snapshot;
let routerState: RouterState = this.router.routerState;
let routerStateSnapshot: RouterStateSnapshot = routerState.snapshot;
console.log(activatedRouteSnapshot);
console.log(routerState);
console.log(routerStateSnapshot);
//如果是从/login这个URL进行的登录,跳转到首页,否则什么都不做
if (routerStateSnapshot.url.indexOf("/login") != -1) {
this.router.navigateByUrl("/home");
}
},
error => console.error(error)
);
this.translate.addLangs(["zh", "en"]);
this.translate.setDefaultLang('zh');
const browserLang = this.translate.getBrowserLang();
console.log("检测到的浏览器语言>" + browserLang);
this.translate.use(browserLang.match(/zh|en/) ? browserLang : 'zh');
}
ngOnDestroy() {
if (this.globalClickCallbackFn) {
this.globalClickCallbackFn();
}
}
toggle(button: any) {
console.log(button);
}
public doLogout(): void {
this.userLoginService.logout();
this.msgs = [];
this.msgs.push({severity:'success', summary:'Success Message', detail:'退出成功'});
this.router.navigateByUrl("");
}
}