Angular's NgShoppingCart is designed in such a way that items stored in the localStorage are automatically cleared out

I am currently working on an Angular project using version 8.0.0.

To integrate a shopping cart feature into my Angular project, I decided to incorporate the NgShoppingCart library by following the instructions provided here.

After adding the library in my app.module, as per the guidelines, I proceeded to add the following code snippet in one of my components:

 constructor(private cartService: CartService<TourCartItem>) {
  }

  ngOnInit() {
  }

  add() {
    const item = new TourCartItem({id: 1, name: 'My item'});
    item.setId(9);
    item.setName('Test item');
    item.setPrice(10);
    item.setQuantity(10);
    this.cartService.addItem(item);
  }

Upon adding an item, everything functions correctly, and I can see the item stored in localStorage.

However, upon reloading the page, all items are reset, which leads me to suspect that there might be an issue with the implementation.

Below is a snippet from my app.module:

@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    SharedModule.forRoot(),
    ShoppingCartModule.forRoot({
      itemType: TourCartItem,
      serviceType: 'localStorage',
      serviceOptions: {storageKey: 'ToursCart', clearOnError: true},
    }),
    CoreModule,
    ServiceWorkerModule.register('/ngsw-worker.js', {enabled: environment.production}),
  ],
  providers: [],
  bootstrap: [CoreComponent]
})
export class AppModule {
  constructor() {
    if (!environment.production) {
      console.log('app');
    }
  }
}

The code for my custom class TourCartItem is shown below:

export class TourCartItem extends CartItem {
  
  // Class definition here
  
}

If anyone has experience with installing and troubleshooting this particular library, your assistance would be greatly appreciated. Thank you in advance.

Answer №1

After resolving the issue, I discovered that the problem stemmed from my custom ItemCart lacking a proper fromJSON implementation.

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

Is Kendo Telerik grid capable of applying conditional formatting while filtering?

Is it feasible to modify the background color of rows in a Kendo Telerik grid when a filter is implemented within an ASP.NET MVC application? ...

What is the best way to exclude an interface using a union type recursively in TypeScript?

I wish to recursively exclude types that are part of union types, and eliminate certain union types Here is an example. Normal and Admin should be considered as union types interface Admin { admin: never; } interface Normal { normal: never; } ...

Send a variable to the npm command's string parameter (and not to the script executed by the npm command)

Is it feasible to include an argument in the npm command string? I have searched through the documentation but couldn't find any relevant information. It would be advantageous for a wide audience to know how to do this. For instance, consider the fol ...

Struggling with your Dropdown Menu onclick functionality in Javascript? Let me lend

I am seeking assistance with implementing a Javascript onclick Dropdown Menu. The current issue I am facing is that when one menu is opened and another menu is clicked, the previous menu remains open. My desired solution is to have the previously open menu ...

When executing `npm run test`, the module 'sonar-request' cannot be located in 'api.js'

Whenever I conduct tests on my Sonarqube plugin, an error is returned if a class makes a call to the api. However, when there is no call to the api, everything works perfectly fine. Below is the error message: FAIL src\main\js\__tests__& ...

Getting a 404 response for incorrect API URLs in ASP.NET Core and single-page applications

In order to properly handle incorrect API calls on the client side with Angular 5, I need to ensure that a 404 error is returned. Currently, the backend is returning a status code of 200 along with index.html, resulting in a JSON parse error on the fronten ...

The property 'filter' is not recognized on the 'Object' type. An attempt to filter the response was made

Trying to fetch data from a JSON file that matches the player's name in the URL, such as localhost:4200/players/Febiven, should only retrieve information about Febiven. The code is written in Angular 6. The current code snippet is as follows: player ...

Differences in Loading Gif Visualization Across Chrome, Safari, and Firefox

Having an issue with a loading image in Chrome, while it displays correctly in Safari and Firefox. In Chrome, I see two half-loading images instead of one whole image. Struggling to find a solution for this problem, any assistance would be greatly apprecia ...

Using NodeJs to Render HTML Templates and Implement Logic on the Server Side

Hello, I am currently working on developing a widget module for my Angular 1 based UI project. This widget is meant to display real-time information. My design involves sending an HTML view to the client from the Node server (potentially using Express.js) ...

Steps for dynamically loading the correct pane without having to refresh the header, footer, or left navigation

Looking to create a web application using HTML, Bootstrap, and jQuery that includes a header, footer, left navigation, and right pane. The content of the right pane will be loaded from a separate HTML page based on what is clicked in the left navigation. ...

Troubleshooting a Custom Pipe Problem in Angular Material Drag and Drop

Currently, I am working on a project involving Angular Material Drag And Drop functionality. I have created a simplified example on StackBlitz which you can access through this link: here The project involves two lists - one containing pets and the other ...

Is there a way for me to retrieve data from a v-for loop in VueJS with the Quasar Framework?

I am currently working on a q-markup-table code to display products based on a search query. I have successfully implemented a button that allows the user to select a row from the table, and once selected, the row data is sent to an array named "selected ...

Angular4 provider being integrated into an AngularJS application offers the provider functionality

I have recently migrated my app from AngularJS to Angular4. Now, I want to create a provider in Angular4 and inject it into the app.config in AngularJS. Here is what I currently have: import { UtilsService } from './../utils/utils.service'; imp ...

What is the proper way to utilize "three.module.js"?

I am currently learning how to utilize modules and decided to start with a simple example. However, I encountered an issue where the script does not want to run. I must be missing something crucial, but I can't seem to figure out what it is. I have tr ...

Having trouble with implementing Nested routes in Vuejs?

main.js import Vue from "vue"; import App from "./App.vue"; import VueRouter from "vue-router"; import HelloWorld from "./components/HelloWorld"; import User from " ...

Is Angular equipped with named routes or states similar to UIRouter?

When working with AngularJS and ui-router, we define a state like this: .state('myState', { url: 'some-user-friendly-url' ... }) This allows us to easily specify the URL and name of the state. It simplifies navigation by letting ...

Setting a CSS Variable with the Help of jQuery

I need help with changing the background color of a specific .div element when it is clicked on. I want the color to be a variable that can be changed by the user, and this change should occur when the document is ready. Currently, I am using a CSS variab ...

A different approach to joining strings

Is there a different method to combine a '#' symbol like in the code snippet below? radioButtonID = '#' + radioButtonID; ...

The Observable<T> generic type must be provided with one type argument

I encountered the following 3 errors while working with the Angular 2 (TypeScript) code below. Can you provide suggestions on how to resolve them? import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { NgModule, Com ...

React/Typescript - Managing various event types within a unified onChange function

In my current project, I am working with a form that includes various types of input fields using the mui library. My goal is to gather the values from these diverse input components and store them in a single state within a Grandparent component. While I ...