Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Angular在NgShoppingCart中重新加载页面后,删除localStorage中的项目_Javascript_Angular_Typescript_Npm - Fatal编程技术网

Javascript 使用Angular在NgShoppingCart中重新加载页面后,删除localStorage中的项目

Javascript 使用Angular在NgShoppingCart中重新加载页面后,删除localStorage中的项目,javascript,angular,typescript,npm,Javascript,Angular,Typescript,Npm,我有一个角度(8.0.0)项目 我添加此库是为了在Angular中实现购物车: 在我将其添加到app.module中之后,如我所解释的,我在一个组件中添加了以下代码行: constructor(private cartService: CartService<TourCartItem>) { } ngOnInit() { } add() { const item = new TourCartItem({id: 1, name: 'My item'})

我有一个角度(8.0.0)项目

我添加此库是为了在Angular中实现购物车:

在我将其添加到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);
  }

}
这是我的自定义类TourCartItem

@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');
    }
  }
}
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;
  }
}
有人能帮忙吗,也许是想安装这个库?
提前感谢。

已修复,问题是我的自定义ItemCart没有正确的fromJSON实现。

请发布更多代码,我想你应该使用LocalStorageCartService类来实现它work@PascalL. 我添加了更多的代码。请解释一下什么是LocalStorageCartService?我没有加。