首页 > 解决方案 > 推送不是功能 - Angular

问题描述

我正在制作一个带角度的应用程序,但出现此错误,推送不是浏览器中的功能。我不明白为什么它会给我这个错误。已经在网上搜索过,但到目前为止没有运气 有人可以帮忙吗?另外,它只发生在其他部分。就像本地存储中已经有产品一样

import { Injectable } from '@angular/core';
import { WebService } from './web.service';
import Product from './models/product';

    @Injectable({
      providedIn: 'root'
    })
    export class ProductsService {
      cartProduct:Product[]
      constructor(private  webService: WebService) { }
      getAllProducts(){
        return this.webService.get('FoodItems');
      }
      getBreadProduct(){
        return this.webService.get('FoodItems/Bread');
      }
      getDairyProduct(){
        return this.webService.get('FoodItems/Dairy');
      }
      getFruitsProduct(){
        return this.webService.get('FoodItems/Fruits');
      }
      getOrganicProduct(){
        return this.webService.get('FoodItems/Organic');
      }
      getVegetablesProducts(){
        return this.webService.get('FoodItems/Vegetables');
      }
      getSeasoningAndSpicesProducts(){
        return this.webService.get('FoodItems/Seasoning-and-spices');
      }
      AddProductInCart(cartP:Product)
      {

        this.cartProduct=this.GetProductInCart()
        if(this.cartProduct==null)
        {
          this.cartProduct=[]
          this.cartProduct.push(cartP)
          localStorage.setItem('cartProduct',JSON.stringify(cartP))

        }
        else{
        this.cartProduct.push(cartP)
        localStorage.setItem('cartProduct',JSON.stringify("cartP"))
      }}
      GetProductInCart()
      {
        return JSON.parse(localStorage.getItem('cartProduct'))
      }

    }

标签: angular

解决方案


替换这一行:

if(this.cartProduct==null)

有了这个:

if(this.cartProduct === null || this.cartProduct === undefined)

或替换此行:

this.cartProduct=this.GetProductInCart()

用这条线:

this.cartProduct=this.GetProductInCart() || [];

推荐阅读