angular - 推送不是功能 - 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'))
}
}
解决方案
替换这一行:
if(this.cartProduct==null)
有了这个:
if(this.cartProduct === null || this.cartProduct === undefined)
或替换此行:
this.cartProduct=this.GetProductInCart()
用这条线:
this.cartProduct=this.GetProductInCart() || [];
推荐阅读
- substrate - 有没有办法调整基板中的块间隔?
- android - Xamarin - 无法通过 Android(和 iOS)访问本地 api
- angular - Angular http get response in observable 在单元测试中不是类型转换
- android - GItlab CI :- 如何使用 Gitlab-CI 在 Android 中创建多个 apk(如开发、登台和生产)?
- python-3.x - Python 脚本返回无
- perl - 使用 Mojo::UserAgent 进行正则表达式匹配的解码结果
- windows - 为多个文件夹中的所有文件添加前缀
- json - Logstash:创建新文件并从 ruby 中的 json 获取动态键值
- typescript - 如何在打字稿的接口/类型中将枚举值类型转换为数组?
- ios - 无法使用共享表 Swift 共享 PDF 文件