admin管理员组文章数量:1023251
I'm trying to add cart item to localStorage in Vue 3. I'm using Composition API and and I'm just learning Vue, so please be understanding :D
Okey, so it's my code of single product view where I call to addProductToCart function
export default {
props: {
id: {
required: true,
type: String
}
},
setup(props) {
const { product, getProduct } = useProducts()
onMounted(getProduct(props.id))
let cartItems = ref([])
function addProductToCart(product) {
cartItems.value.push({...product})
window.localStorage.setItem('CART', JSON.stringify(cartItems.value))
console.log(JSON.parse(localStorage.getItem('CART')));
}
return {
addProductToCart,
product
}
}
}
Problem is when I reload page. State of cart items it's stored but when I add another product after refreshing page the localStorage it's reset.
Problem it's here:
let cartItems = ref([])
Im trying to check if the localStorage is not null and when is not null I want add another products to exsisting state of cart product items so trying like this:
let cartItems = ref([])
if (window.localStorage.getItem('CART') !== null) {
cartItems = window.localStorage.getItem('CART')
}
But when I click ADD TO CART button I receive error:
Uncaught TypeError: Cannot read properties of undefined (reading 'push')
I searched the internet for a lot of information about the state of carts but I found nothing sensible where they represent storage of cart states using the Composition API.
Maybe some of you know some cool material that will show you how to properly create a shopping cart using the Composition API.
Thanks!
I'm trying to add cart item to localStorage in Vue 3. I'm using Composition API and and I'm just learning Vue, so please be understanding :D
Okey, so it's my code of single product view where I call to addProductToCart function
export default {
props: {
id: {
required: true,
type: String
}
},
setup(props) {
const { product, getProduct } = useProducts()
onMounted(getProduct(props.id))
let cartItems = ref([])
function addProductToCart(product) {
cartItems.value.push({...product})
window.localStorage.setItem('CART', JSON.stringify(cartItems.value))
console.log(JSON.parse(localStorage.getItem('CART')));
}
return {
addProductToCart,
product
}
}
}
Problem is when I reload page. State of cart items it's stored but when I add another product after refreshing page the localStorage it's reset.
Problem it's here:
let cartItems = ref([])
Im trying to check if the localStorage is not null and when is not null I want add another products to exsisting state of cart product items so trying like this:
let cartItems = ref([])
if (window.localStorage.getItem('CART') !== null) {
cartItems = window.localStorage.getItem('CART')
}
But when I click ADD TO CART button I receive error:
Uncaught TypeError: Cannot read properties of undefined (reading 'push')
I searched the internet for a lot of information about the state of carts but I found nothing sensible where they represent storage of cart states using the Composition API.
Maybe some of you know some cool material that will show you how to properly create a shopping cart using the Composition API.
Thanks!
Share Improve this question asked Dec 7, 2021 at 11:20 AdrianAdrian 3994 silver badges10 bronze badges2 Answers
Reset to default 3you need to set the ref value and also to parse the local storage value
let cartItems = ref([])
if (window.localStorage.getItem('CART') !== null) {
cartItems.value = JSON.parse(window.localStorage.getItem('CART'));
}
I believe you should use cartItems.value in your code:
let cartItems = ref([])
if (window.localStorage.getItem('CART') !== null) {
cartItems.value = window.localStorage.getItem('CART')
}
I'm trying to add cart item to localStorage in Vue 3. I'm using Composition API and and I'm just learning Vue, so please be understanding :D
Okey, so it's my code of single product view where I call to addProductToCart function
export default {
props: {
id: {
required: true,
type: String
}
},
setup(props) {
const { product, getProduct } = useProducts()
onMounted(getProduct(props.id))
let cartItems = ref([])
function addProductToCart(product) {
cartItems.value.push({...product})
window.localStorage.setItem('CART', JSON.stringify(cartItems.value))
console.log(JSON.parse(localStorage.getItem('CART')));
}
return {
addProductToCart,
product
}
}
}
Problem is when I reload page. State of cart items it's stored but when I add another product after refreshing page the localStorage it's reset.
Problem it's here:
let cartItems = ref([])
Im trying to check if the localStorage is not null and when is not null I want add another products to exsisting state of cart product items so trying like this:
let cartItems = ref([])
if (window.localStorage.getItem('CART') !== null) {
cartItems = window.localStorage.getItem('CART')
}
But when I click ADD TO CART button I receive error:
Uncaught TypeError: Cannot read properties of undefined (reading 'push')
I searched the internet for a lot of information about the state of carts but I found nothing sensible where they represent storage of cart states using the Composition API.
Maybe some of you know some cool material that will show you how to properly create a shopping cart using the Composition API.
Thanks!
I'm trying to add cart item to localStorage in Vue 3. I'm using Composition API and and I'm just learning Vue, so please be understanding :D
Okey, so it's my code of single product view where I call to addProductToCart function
export default {
props: {
id: {
required: true,
type: String
}
},
setup(props) {
const { product, getProduct } = useProducts()
onMounted(getProduct(props.id))
let cartItems = ref([])
function addProductToCart(product) {
cartItems.value.push({...product})
window.localStorage.setItem('CART', JSON.stringify(cartItems.value))
console.log(JSON.parse(localStorage.getItem('CART')));
}
return {
addProductToCart,
product
}
}
}
Problem is when I reload page. State of cart items it's stored but when I add another product after refreshing page the localStorage it's reset.
Problem it's here:
let cartItems = ref([])
Im trying to check if the localStorage is not null and when is not null I want add another products to exsisting state of cart product items so trying like this:
let cartItems = ref([])
if (window.localStorage.getItem('CART') !== null) {
cartItems = window.localStorage.getItem('CART')
}
But when I click ADD TO CART button I receive error:
Uncaught TypeError: Cannot read properties of undefined (reading 'push')
I searched the internet for a lot of information about the state of carts but I found nothing sensible where they represent storage of cart states using the Composition API.
Maybe some of you know some cool material that will show you how to properly create a shopping cart using the Composition API.
Thanks!
Share Improve this question asked Dec 7, 2021 at 11:20 AdrianAdrian 3994 silver badges10 bronze badges2 Answers
Reset to default 3you need to set the ref value and also to parse the local storage value
let cartItems = ref([])
if (window.localStorage.getItem('CART') !== null) {
cartItems.value = JSON.parse(window.localStorage.getItem('CART'));
}
I believe you should use cartItems.value in your code:
let cartItems = ref([])
if (window.localStorage.getItem('CART') !== null) {
cartItems.value = window.localStorage.getItem('CART')
}
本文标签: javascriptHow to add item to local storage Vue 3 with Composition APIStack Overflow
版权声明:本文标题:javascript - How to add item to local storage Vue 3 with Composition API - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745512340a2153880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论