英文:
Angular 16 routing blank page
问题
I set up the routes with angular v16 and got blank page for login and register I'm pretty sure it's with the routing but I checked it million times and didn't figure it out where is the issue exactly, this is the code snippets:
这是我使用 Angular v16 设置的路由,但登录和注册页面都是空白的,我非常确定问题出在路由上,但我已经检查了很多次,仍然无法找出问题所在,以下是代码片段:
this is the access service
这是访问服务的代码:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Login } from "../../model/login";
import { Register } from "../../model/register";
import { JwtHelperService } from '@auth0/angular-jwt';
@Injectable({
providedIn: 'root'
})
export class AccessService {
private baseUrl = 'http://localhost:8096';
constructor(private http: HttpClient, private jwtHelper: JwtHelperService) { }
login(loginData: Login): Observable<any> {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post(`${this.baseUrl}/login`, loginData, { headers });
}
register(registerData: Register): Observable<any> {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post(`${this.baseUrl}/customerRegister`, registerData, { headers });
}
saveTokens(tokens: { jwtToken: string, refreshToken: string }): void {
localStorage.setItem('jwtToken', tokens.jwtToken);
localStorage.setItem('refreshToken', tokens.refreshToken);
}
getJwtToken(): string | null {
return localStorage.getItem('jwtToken');
}
getRefreshToken(): string | null {
return localStorage.getItem('refreshToken');
}
removeTokens(): void {
localStorage.removeItem('jwtToken');
localStorage.removeItem('refreshToken');
}
isTokenExpired(): boolean {
const jwtToken = this.getJwtToken();
return jwtToken ? this.jwtHelper.isTokenExpired(jwtToken) : true;
}
}
This is the app routes:
这是应用程序的路由配置:
export const routes: Routes = [
{ path: '', redirectTo: '/main', pathMatch: 'full' },
{ path: 'main', component: MainComponent },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'otp', component: OtpComponent },
{ path: 'otp-reset', component: OtpResetComponent },
{ path: 'settings', component: SettingsComponent },
{ path: 'shop', component: ShopComponent },
{ path: 'product-detail', component: ProdDetailComponent },
{ path: 'cart', component: AddCartComponent },
{ path: 'checkout', component: CheckoutComponent },
];
And finally, this is the register component:
最后,这是注册组件的代码:
@Component({
selector: 'app-register',
standalone: true,
imports: [CommonModule, FormsModule, HttpClientModule],
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent {
constructor(private accessService: AccessService, private router: Router) { }
onSubmit(registerData: Register): void {
this.accessService.register(registerData).subscribe(
() => {
// Handle the successful registration response
Swal.fire({
icon: 'success',
title: 'تم التسجيل',
text: 'تمت عملية التسجيل بنجاح!',
}).then(() => {
this.router.navigate(['/shop']);
});
}
);
}
}
Also, I connect it directly to the backend API, but I checked it also, and the problem is in Angular, not in the backend.
此外,我直接连接到了后端 API,但我也进行了检查,问题出在 Angular,而不是后端。
英文:
I set up the routes with angular v16 and got blank page for login and register I'm pretty sure it's with the routing but I checked it million time and didn't figure it out where is the issue exactly, this is the code snippets:
this is the access service
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import {Login} from "../../model/login";
import {Register} from "../../model/register";
import { JwtHelperService } from '@auth0/angular-jwt';
@Injectable({
providedIn: 'root'
})
export class AccessService {
private baseUrl = 'http://localhost:8096';
constructor(private http: HttpClient, private jwtHelper: JwtHelperService) { }
login(loginData: Login): Observable<any> {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post(`${this.baseUrl}/login`, loginData, { headers });
}
register(registerData: Register): Observable<any> {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post(`${this.baseUrl}/customerRegister`, registerData, { headers });
}
saveTokens(tokens: { jwtToken: string, refreshToken: string }): void {
localStorage.setItem('jwtToken', tokens.jwtToken);
localStorage.setItem('refreshToken', tokens.refreshToken);
}
getJwtToken(): string | null {
return localStorage.getItem('jwtToken');
}
getRefreshToken(): string | null {
return localStorage.getItem('refreshToken');
}
removeTokens(): void {
localStorage.removeItem('jwtToken');
localStorage.removeItem('refreshToken');
}
isTokenExpired(): boolean {
const jwtToken = this.getJwtToken();
return jwtToken ? this.jwtHelper.isTokenExpired(jwtToken) : true;
}
}
This is the app routes:
export const routes: Routes = [
{ path: '', redirectTo: '/main', pathMatch: 'full' },
{ path: 'main', component: MainComponent },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'otp', component: OtpComponent },
{ path: 'otp-reset', component: OtpResetComponent },
{ path: 'settings', component: SettingsComponent },
{ path: 'shop', component: ShopComponent },
{ path: 'product-detail', component: ProdDetailComponent },
{ path: 'cart', component: AddCartComponent },
{ path: 'checkout', component: CheckoutComponent },
];
And finally this is the register componenet:
@Component({
selector: 'app-register',
standalone: true,
imports: [CommonModule, FormsModule, HttpClientModule],
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent {
constructor(private accessService: AccessService, private router: Router) { }
onSubmit(registerData: Register): void {
this.accessService.register(registerData).subscribe(
() => {
// Handle the successful registration response
Swal.fire({
icon: 'success',
title: 'تم التسجيل',
text: 'تمت عملية التسجيل بنجاح!',
}).then(() => {
this.router.navigate(['/shop']);
});
}
);
}
}
Also I connect it directly to the backend api I checked it also but the problem in angular not in the backend
trying to get endpoint from backend api
答案1
得分: 1
你有一个依赖问题。需要在你的 standalone
组件中导入 HttpClientModule
。
@Component({
selector: 'app-login',
standalone: true,
imports: [CommonModule, HttpModuleClient], // 你需要在这里导入你的模块
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
英文:
You have a dependency issue. HttpClientModule
needs to be imported in your standalone
component.
@Component({
selector: 'app-login',
standalone: true,
imports: [CommonModule, HttpModuleClient], // You need to import your modules here
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论