本地存储数据未呈现
local storage data not rendered
目前我正在制作一个电子商务网站。我想按用户将订单数据存储在本地存储中。这是我的代码:
details.vue
import DataService from '../web_service/services'
export default {
name: 'details',
props: ['userId'],
data () {
return {
datas: null,
url: '/' + this.userId,
value: 1
}
},
created () {
DataService.getFindById(this.url)
.then((res) => {
this.datas = res.data.data
})
.catch((err) => {
alert('error when fetching API' + err)
})
},
methods: {
buyNow () {
let order = {
product: this.datas.name,
price: this.datas.price,
quantity: this.value
}
this.$store.commit('addOrder', order)
this.$router.push('/cart')
}
}
}
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
order: JSON.parse(localStorage.getItem("order")),
},
mutations: {
addOrder: (state, product) => {
const arrProduct = []
const parse = JSON.stringify(product)
state.order = parse
localStorage.setItem('order', parse)
}
}
})
shopCart.vue
<template>
<div class="shopCart>
<b-card-body>
<b-card-text>
<div v-if="orders" class="float-left">
<h5>{{orders.product}}</h5>
<h5>{{orders.price}}</h5>
</div>
</b-card-text>
</b-card-body>
</div>
</template>
<script>
export default {
name: 'shopCart',
data () {
return {
orders: null
}
},
created () {
this.orders = this.$store.state.order
}
}
</script>
当我尝试接受订单时,它直接指向 /cart
路线,然后显示我的商品。但是,我的订单数据没有呈现。当我刷新页面时,它会显示我的订单数据。有谁知道解决方案?提前致谢!
一切正常,只是您将 order
设置为用于 localStorage
的已解析字符串。因此,当您查看购物车页面时,orders.product
和 orders.price
未定义,即使 v-if
是 true
。
改变你的突变:
addOrder: (state, product) => {
const arrProduct = []
const parse = JSON.stringify(product)
state.order = product; // ✅ Set this to the product, not a string
localStorage.setItem('order', parse)
}
目前我正在制作一个电子商务网站。我想按用户将订单数据存储在本地存储中。这是我的代码:
details.vue
import DataService from '../web_service/services'
export default {
name: 'details',
props: ['userId'],
data () {
return {
datas: null,
url: '/' + this.userId,
value: 1
}
},
created () {
DataService.getFindById(this.url)
.then((res) => {
this.datas = res.data.data
})
.catch((err) => {
alert('error when fetching API' + err)
})
},
methods: {
buyNow () {
let order = {
product: this.datas.name,
price: this.datas.price,
quantity: this.value
}
this.$store.commit('addOrder', order)
this.$router.push('/cart')
}
}
}
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
order: JSON.parse(localStorage.getItem("order")),
},
mutations: {
addOrder: (state, product) => {
const arrProduct = []
const parse = JSON.stringify(product)
state.order = parse
localStorage.setItem('order', parse)
}
}
})
shopCart.vue
<template>
<div class="shopCart>
<b-card-body>
<b-card-text>
<div v-if="orders" class="float-left">
<h5>{{orders.product}}</h5>
<h5>{{orders.price}}</h5>
</div>
</b-card-text>
</b-card-body>
</div>
</template>
<script>
export default {
name: 'shopCart',
data () {
return {
orders: null
}
},
created () {
this.orders = this.$store.state.order
}
}
</script>
当我尝试接受订单时,它直接指向 /cart
路线,然后显示我的商品。但是,我的订单数据没有呈现。当我刷新页面时,它会显示我的订单数据。有谁知道解决方案?提前致谢!
一切正常,只是您将 order
设置为用于 localStorage
的已解析字符串。因此,当您查看购物车页面时,orders.product
和 orders.price
未定义,即使 v-if
是 true
。
改变你的突变:
addOrder: (state, product) => {
const arrProduct = []
const parse = JSON.stringify(product)
state.order = product; // ✅ Set this to the product, not a string
localStorage.setItem('order', parse)
}