将登录页面放在主页中
Putting the Log in Page in the Home Page
我是 JHipster 的新手,我想知道是否有办法将登录页面“合并”到 JHipster 的主页。
到目前为止,我的 HTML 看起来像这样:
但是一旦我合并了 login.component.ts
和 home.component.ts
的代码。它看起来像这样:
我真的不知道出了什么问题,我也没有任何错误。
我的 home.component.ts
看起来像这样:
import { Component, OnInit, OnDestroy,AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { Subscription } from 'rxjs';
import { LoginModalService } from 'app/core/login/login-modal.service';
import { AccountService } from 'app/core/auth/account.service';
import { Account } from 'app/core/user/account.model';
import { FormBuilder } from '@angular/forms';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { Router } from '@angular/router';
import { LoginService } from 'app/core/login/login.service';
@Component({
selector: 'jhi-home',
templateUrl: './home.component.html',
styleUrls: ['home.scss'],
})
export class HomeComponent implements AfterViewInit ,OnInit, OnDestroy {
account: Account | null = null;
authSubscription?: Subscription;
@ViewChild('username', { static: false })
username?: ElementRef;
authenticationError = false;
loginForm = this.fb.group({
username: [''],
password: [''],
rememberMe: [false],
});
constructor(private accountService: AccountService, private loginModalService: LoginModalService,private loginService: LoginService, private router: Router, public activeModal: NgbActiveModal, private fb: FormBuilder) {}
ngAfterViewInit(): void {
if (this.username) {
this.username.nativeElement.focus();
}
}
cancel(): void {
this.authenticationError = false;
this.loginForm.patchValue({
username: '',
password: '',
});
this.activeModal.dismiss('cancel');
}
login(): void {
this.loginService
.login({
username: this.loginForm.get('username')!.value,
password: this.loginForm.get('password')!.value,
rememberMe: this.loginForm.get('rememberMe')!.value,
})
.subscribe(
() => {
this.authenticationError = false;
this.activeModal.close();
if (
this.router.url === '/account/register' ||
this.router.url.startsWith('/account/activate') ||
this.router.url.startsWith('/account/reset/')
) {
this.router.navigate(['']);
}
},
() => (this.authenticationError = true)
);
}
register(): void {
this.activeModal.dismiss('to state register');
this.router.navigate(['/account/register']);
}
requestResetPassword(): void {
this.activeModal.dismiss('to state requestReset');
this.router.navigate(['/account/reset', 'request']);
}
ngOnInit(): void {
this.authSubscription = this.accountService.getAuthenticationState().subscribe(account => (this.account = account));
}
isAuthenticated(): boolean {
return this.accountService.isAuthenticated();
}
ngOnDestroy(): void {
if (this.authSubscription) {
this.authSubscription.unsubscribe();
}
}
}
我的代码有错误吗?谢谢
好的,我明白了,问题出在 NgbActiveModal,我们只是不必在 home 构造函数中初始化它,一切正常!
我是 JHipster 的新手,我想知道是否有办法将登录页面“合并”到 JHipster 的主页。 到目前为止,我的 HTML 看起来像这样:
但是一旦我合并了 login.component.ts
和 home.component.ts
的代码。它看起来像这样:
我真的不知道出了什么问题,我也没有任何错误。
我的 home.component.ts
看起来像这样:
import { Component, OnInit, OnDestroy,AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { Subscription } from 'rxjs';
import { LoginModalService } from 'app/core/login/login-modal.service';
import { AccountService } from 'app/core/auth/account.service';
import { Account } from 'app/core/user/account.model';
import { FormBuilder } from '@angular/forms';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { Router } from '@angular/router';
import { LoginService } from 'app/core/login/login.service';
@Component({
selector: 'jhi-home',
templateUrl: './home.component.html',
styleUrls: ['home.scss'],
})
export class HomeComponent implements AfterViewInit ,OnInit, OnDestroy {
account: Account | null = null;
authSubscription?: Subscription;
@ViewChild('username', { static: false })
username?: ElementRef;
authenticationError = false;
loginForm = this.fb.group({
username: [''],
password: [''],
rememberMe: [false],
});
constructor(private accountService: AccountService, private loginModalService: LoginModalService,private loginService: LoginService, private router: Router, public activeModal: NgbActiveModal, private fb: FormBuilder) {}
ngAfterViewInit(): void {
if (this.username) {
this.username.nativeElement.focus();
}
}
cancel(): void {
this.authenticationError = false;
this.loginForm.patchValue({
username: '',
password: '',
});
this.activeModal.dismiss('cancel');
}
login(): void {
this.loginService
.login({
username: this.loginForm.get('username')!.value,
password: this.loginForm.get('password')!.value,
rememberMe: this.loginForm.get('rememberMe')!.value,
})
.subscribe(
() => {
this.authenticationError = false;
this.activeModal.close();
if (
this.router.url === '/account/register' ||
this.router.url.startsWith('/account/activate') ||
this.router.url.startsWith('/account/reset/')
) {
this.router.navigate(['']);
}
},
() => (this.authenticationError = true)
);
}
register(): void {
this.activeModal.dismiss('to state register');
this.router.navigate(['/account/register']);
}
requestResetPassword(): void {
this.activeModal.dismiss('to state requestReset');
this.router.navigate(['/account/reset', 'request']);
}
ngOnInit(): void {
this.authSubscription = this.accountService.getAuthenticationState().subscribe(account => (this.account = account));
}
isAuthenticated(): boolean {
return this.accountService.isAuthenticated();
}
ngOnDestroy(): void {
if (this.authSubscription) {
this.authSubscription.unsubscribe();
}
}
}
我的代码有错误吗?谢谢
好的,我明白了,问题出在 NgbActiveModal,我们只是不必在 home 构造函数中初始化它,一切正常!