You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
pocketcommunityweb/src/app/app.component.ts

56 lines
1.6 KiB

import {Component} from '@angular/core';
import {Router, ActivatedRoute, NavigationStart} from '@angular/router';
import {TranslateService} from '@ngx-translate/core';
import { filter } from 'rxjs/operators';
import { Observable } from 'rxjs-compat';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
// 登陆状态
isLogin;
navStart: Observable<NavigationStart>;
constructor(public route: ActivatedRoute, public translate: TranslateService, private router: Router) {
// 导航开始事件
this.navStart = this.router.events.pipe(
filter(evt => evt instanceof NavigationStart)
) as Observable<NavigationStart>;
}
public async ngOnInit() {
// 语言初始化(若未设置语言, 则取浏览器语言)
const currentLanguage = await localStorage.getItem('currentLanguage') || this.translate.getBrowserCultureLang();
// 当在assets/i18n中找不到对应的语言翻译时,使用'zh-CN'作为默认语言
this.translate.setDefaultLang('zh-CN');
this.translate.use(currentLanguage);
// 记录当前设置的语言
localStorage.setItem('currentLanguage', currentLanguage);
// const that = this;
// this.navStart.subscribe(evt => {
// if ( evt.url !== '/login') {
// // 检查登陆状态
// this.checkLogin(that);
// }
// });
this.checkLogin();
}
/**
* 检查登陆状态
*/
checkLogin() {
this.isLogin = true;
if (this.isLogin) {
this.router.navigateByUrl('/forum');
} else {
this.router.navigateByUrl('/login');
}
}
}