Angular 16 routing not loading content

I configured the routes in Angular v16 and encountered a blank page issue with the login and register functionality. I suspect it has to do with routing, but after checking multiple times, I couldn't pinpoint the exact problem. Below are snippets of the code:

Here is the access service implementation:

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;
  }
}

Below you can find the application routes configuration:

export const routes: Routes = [
  { path: '', redirectTo: '/main', pathMatch: 'full' },
  // Define all other paths here
];

Lastly, here is the register component implementation:

@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 {
    // Handle registration logic here
  }
}

I have verified the connection to the backend API and confirmed that the issue lies within the Angular setup rather than the backend. Additionally, I am trying to retrieve endpoints from the backend API.

Answer №1

You are facing a dependency issue that requires importing the HttpClientModule in your standalone component.

@Component({
  selector: 'app-login',
  standalone: true,
  imports: [CommonModule, HttpModuleClient], // Make sure to import necessary modules here
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

TypeScript React Object.assign method return type

I have a unique custom function that utilizes Object.assign to return a specific result. The documentation mentions that this function returns an array, but surprisingly, it can be destructured both as an array and an object. Check out the code snippet be ...

Exploring the functionality of surveyjs in conjunction with react and typescript

Does anyone have any code samples showcasing how to integrate Surveyjs with React and TypeScript? I attempted to import it into my project and utilized the code provided in this resource. https://stackblitz.com/edit/surveyjs-react-stackoverflow45544026 H ...

Guide to incorporating Jquery-Comment into Angular versions 2 and beyond

I've incorporated the Jquery-Comment plugin into my Angular 2 project by adding the necessary script and css files to my Index.html page. However, when initializing the Comment TextBox id within a script tag, I encountered an error in the console. ...

Bringing in TypeScript declarations for the compiled JavaScript librarybundle

I have a custom library written in TypeScript with multiple files and an index.ts file that handles all the exports. To consolidate the library, I used webpack to compile it into a single index.js file but now I'm facing challenges importing it with ...

Error occurs with navctrl when using jquery in Ionic 3

Recently, I started using Ionic framework, I have been integrating jQuery in Ionic to make REST API calls. However, whenever I try to use navCtrl with jQuery, an error pops up in the Chrome console, ERROR TypeError: Cannot read property 'push' ...

Disabling `no-dupe-keys` in ESLint does not seem to be effective

Currently, I am working on a project where I have incorporated Typescript and ESLint. However, I have encountered an issue with the error message stating: An object literal cannot have multiple properties with the same name. I am looking to disable this s ...

There was an unexpected error: Module 'C:Users... ode_modules@angular-devkituild-angularsrcdev-server' could not be located

Struggling to get my Angular project up and running on a different system. I've copied all the necessary files (hopefully) but when I attempt to run the project using ng serve, it throws this error: An unhandled exception occurred: Cannot find module ...

Handling Click and Mouse Events with React [react-sortable-hoc, material-ui, react-virtualized]

I have come across an interesting example that I would like to share with you. Check out this live working example on Stackblitz When the delete button on the red bin icon is pressed, the onClick event handler does not get triggered (sorting happens inst ...

Tips for implementing a method to switch CSS properties of a main container by using a checkbox within its child element in a Svelte component

It took me a while to figure this out, but I still feel like my implementation is not ideal. I'm confused as to why things break when I remove the checkedActivities.has(activity) ? "checked" : "unchecked", because I thought TypeScr ...

The string retrieved from the server is showing special characters as "?"

I am facing an issue with my servlet where it retrieves data from Cloud SQL, converts it to a Java object, and then converts it to JSON before appending it to the response writer. The problem arises when the data contains special characters like "ç" and ...

VS Code is flagging TypeScript errors following the recent software update

After updating my VS Code, I started seeing TypeScript error messages like the following: ButtonUnstyled.types.d.ts: Module '"/components/node_modules/@types/react/index"' can only be default-imported using the 'esModuleInterop&a ...

Using Systemjs with Angular 2 results in 50 server calls for loading resources

While following the Angular2 quickstart tutorial on Angular.io, I noticed that it was making 50 separate requests, which left me wondering why. Is there a way to consolidate all these requests into one? My goal is to have a maximum of 8 bundles. This is ...

I am attempting to override the style class of a p-tag in Primeng, but I am finding that the :host ::ng-deep selector

Check out the following code snippets: component-xyz.html <td> <p-tag styleClass = "p-mr-2" severity = "{{getSeverity(status)}}" *ngIf = "status" [ngClass] = "status"></p-tag></td> component ...

Having trouble connecting my chosen color from the color picker

Currently, I am working on an angularJS typescript application where I am trying to retrieve a color from a color picker. While I am successfully obtaining the value from the color picker, I am facing difficulty in binding this color as a background to my ...

Unable to display information stored in the Firebase database

I am struggling with a frustrating issue. My goal is to showcase the information stored in the Firebase database in a clear and organized manner, but I'm having trouble achieving this because it's being treated as an object. getData(){ firebas ...

The error message "Property 'name' does not exist on type 'User'" is encountered

When running this code, I expected my form to display in the browser. However, I encountered an error: Error: src/app/addproducts/addproducts.component.html:18:48 - error TS2339: Property 'price' does not exist on type 'ADDPRODUCTSComponent& ...

How can I effectively utilize the Metamask SDK with TypeScript?

Currently, I am in the process of developing a webpack+pnpm+typescript+react website. All the versions being used are LTS and my preferred IDE is VSCode. According to the guide provided by Metamask here, it seems like I need to follow these steps: npm i @m ...

Upon updating from Angular5 to Angular7, the slice pipe seems to be stuck in an endless loop

Recently, I upgraded my project from angular 5 to angular 7. However, after completing the upgrade, I noticed that the slice pipe was not functioning correctly. It seemed to be causing infinite loops in the angular core files and even resulted in my browse ...

Angular 4: Prevent the HTML template of the parent component from being rendered in the

My component has the following routes: { path: ':id', component: ProjectDetailsComponent, children: [ {path: '', redirectTo: 'gathering', pathMatch: 'full'}, {path: ' ...

Guide to Utilizing the Dracula Graph Library in Angular

Recently, I stumbled upon a JavaScript library that seems to be an ideal fit for my project. The library can be found at: After installing the necessary libraries using npm - npm i raphael graphdracula - new folders were created in node_modules and th ...