У меня есть проект Angular (8.0.0).
Я добавил эту библиотеку для реализации корзины покупок в Angular:
NgShoppingCart
После того, как я добавил его в app.module как объяснено здесь Я добавил эти строки кода в один компонент:
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);
}
}
Все работает, когда я добавляю элемент, я вижу это в localStorage:

но после перезагрузки страницы элементы сбрасываются:

Это мой 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');
}
}
}
Это мой пользовательский класс TourCartItem :
export class TourCartItem extends CartItem {
uuid: any;
description: string;
name: string;
price: number;
image: string;
quantity: number;
data: any;
constructor(itemData?: any) {
super();
if (itemData) {
this.uuid = itemData.uuid;
this.description = itemData.description;
this.name = itemData.name;
this.price = itemData.price;
this.image = itemData.image;
this.quantity = itemData.quantity;
this.data = itemData.data;
}
}
static fromJSON(itemData: any) {
return new TourCartItem(itemData.uuid);
}
getId(): any {
return this.uuid;
}
setId(id: any): void {
this.uuid = id;
}
getDescription(): any {
return this.description;
}
setDescription(description: any): any {
this.description = description;
}
getName(): string {
return this.name;
}
setName(name: string): void {
this.name = name;
}
getPrice(): number {
return this.price;
}
setPrice(price: number): void {
this.price = price;
}
getQuantity(): number {
return this.quantity;
}
setQuantity(quantity: number): void {
this.quantity = quantity;
}
getImage(): string {
return this.image;
}
setImage(image: string): void {
this.image = image;
}
getData(): any {
return this.data;
}
setData(data: any): void {
this.data = data;
}
}
Может кто-нибудь помочь, может быть, пытается установить эту библиотеку?
Заранее спасибо.