P粉6656790532023-08-31 11:46:54
vue/no-async-in-computed-properties
Properties are not captured in templates, but lifecycle hooks can obtain data through calculated properties
The reason you see that in the console is because most modern browsers log objects as live data (once the object is updated, the console is updated too). So what you see in the console is not the value of the object when console.log
is executed, but the value later. You can confirm this by using console.log(JSON.parse(JSON.stringify(this.products)))
...
To solve this problem, use watch
instead of computed
data() { return { products: [] } }, watch: { '$route.query.pid': { handler: function(newValue) { if(newValue) { axios.get(`/api/products/${newValue}`).then(response => { var product = { id: response.data[0].id, name: response.data[0].name, units: response.data[0].units image: response.data[0].product_image[0].image price: response.data[0].price quantity: 1 } this.products = [] this.products.push(product) } else this.products = this.$store.state.cart }, immediate: true } },