Angular 16路由空白页面

huangapple go评论77阅读模式
英文:

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 &#39;@angular/core&#39;;
import { HttpClient, HttpHeaders } from &#39;@angular/common/http&#39;;
import { Observable } from &#39;rxjs&#39;;
import {Login} from &quot;../../model/login&quot;;
import {Register} from &quot;../../model/register&quot;;
import { JwtHelperService } from &#39;@auth0/angular-jwt&#39;;

@Injectable({
  providedIn: &#39;root&#39;
})
export class AccessService {
  private baseUrl = &#39;http://localhost:8096&#39;;

  constructor(private http: HttpClient, private jwtHelper: JwtHelperService) { }

  login(loginData: Login): Observable&lt;any&gt; {
    const headers = new HttpHeaders({ &#39;Content-Type&#39;: &#39;application/json&#39; });
    return this.http.post(`${this.baseUrl}/login`, loginData, { headers });
  }

  register(registerData: Register): Observable&lt;any&gt; {
    const headers = new HttpHeaders({ &#39;Content-Type&#39;: &#39;application/json&#39; });
    return this.http.post(`${this.baseUrl}/customerRegister`, registerData, { headers });
  }

  saveTokens(tokens: { jwtToken: string, refreshToken: string }): void {
    localStorage.setItem(&#39;jwtToken&#39;, tokens.jwtToken);
    localStorage.setItem(&#39;refreshToken&#39;, tokens.refreshToken);
  }

  getJwtToken(): string | null {
    return localStorage.getItem(&#39;jwtToken&#39;);
  }

  getRefreshToken(): string | null {
    return localStorage.getItem(&#39;refreshToken&#39;);
  }

  removeTokens(): void {
    localStorage.removeItem(&#39;jwtToken&#39;);
    localStorage.removeItem(&#39;refreshToken&#39;);
  }

  isTokenExpired(): boolean {
    const jwtToken = this.getJwtToken();
    return jwtToken ? this.jwtHelper.isTokenExpired(jwtToken) : true;
  }
}

This is the app routes:

export const routes: Routes = [
  { path: &#39;&#39;, redirectTo: &#39;/main&#39;, pathMatch: &#39;full&#39; },
  { path: &#39;main&#39;, component: MainComponent },
  { path: &#39;login&#39;, component: LoginComponent },
  { path: &#39;register&#39;, component: RegisterComponent },
  { path: &#39;otp&#39;, component: OtpComponent },
  { path: &#39;otp-reset&#39;, component: OtpResetComponent },
  { path: &#39;settings&#39;, component: SettingsComponent },
  { path: &#39;shop&#39;, component: ShopComponent },
  { path: &#39;product-detail&#39;, component: ProdDetailComponent },
  { path: &#39;cart&#39;, component: AddCartComponent },
  { path: &#39;checkout&#39;, component: CheckoutComponent },
];

And finally this is the register componenet:

@Component({
  selector: &#39;app-register&#39;,
  standalone: true,
  imports: [CommonModule, FormsModule, HttpClientModule],
  templateUrl: &#39;./register.component.html&#39;,
  styleUrls: [&#39;./register.component.css&#39;]
})
export class RegisterComponent {

  constructor(private accessService: AccessService, private router: Router) { }

  onSubmit(registerData: Register): void {
    this.accessService.register(registerData).subscribe(
      () =&gt; {
        // Handle the successful registration response
        Swal.fire({
          icon: &#39;success&#39;,
          title: &#39;تم التسجيل&#39;,
          text: &#39;تمت عملية التسجيل بنجاح!&#39;,
        }).then(() =&gt; {
          this.router.navigate([&#39;/shop&#39;]);
        });
      }
    );
  }
}

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: &#39;app-login&#39;,
  standalone: true,
  imports: [CommonModule, HttpModuleClient], // You need to import your modules here
  templateUrl: &#39;./login.component.html&#39;,
  styleUrls: [&#39;./login.component.css&#39;]
})
export class LoginComponent implements OnInit {

huangapple
  • 本文由 发表于 2023年5月22日 20:25:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定