【发布时间】:2018-05-10 09:35:47
【问题描述】:
嗯,
我正在尝试使用 vue.js 做一个购物车项目,浏览器控制台显示此错误:
vue.common.js:576 [Vue warn]: Error in created hook: "TypeError: Cannot use 'in' operator to search '[object Array]' in products"
// App.vue
<template>
<div class="container">
<div class="products">
<div class="clearfix">
<product v-for="product in products" :key="product"></product>
</div>
</div>
<div class="shopping-cart">
<shopping-cart></shopping-cart>
</div>
</div>
</template>
<script>
import ShoppingCart from './components/ShoppingCart.vue'
import Product from './components/Product.vue'
export default {
created () {
// dados mockados
var dummy = [
{id: 1, title: 'Name of Product 1', price: 40, image: 'product.png'},
{id: 2, title: 'Name of Product 2', price: 90, image: 'product.png'},
{id: 3, title: 'Name of Product 3', price: 10, image: 'product.png'},
{id: 4, title: 'Name of Product 4', price: 20, image: 'product.png'}
];
this.$set('products', dummy)
},
data () {
return {
products: []
}
},
components: { Product, ShoppingCart }
}
</script>
我能做什么?
我尝试了很多东西,但仍然没有成功=(
【问题讨论】:
-
推入产品数组而不是$set,
this.products.push({id: 1, title: 'Name of Product 1', price: 40, image: 'product.png'}, { the others })
标签: javascript vue.js