【发布时间】:2018-04-21 12:38:33
【问题描述】:
我有一个包含数组和对象的父组件:
data() {
return {
products: [],
attributes: {},
}
},
加载我的父组件时,它会获取一些 AJAX 数据并填充变量:
mounted() {
axios.get('/requests/getProducts/' + this.currentCategory).then(response => {
this.products = response.data;
this.createAttributes(); // runs some methods to populate the attributes object
});
},
然后,我将使用一个子组件来显示属性。来自父模板的代码:
<search-attributes :attributes="attributes"></search-attributes>
我的子组件中声明了道具:
props: ['attributes'],
到目前为止一切顺利,我可以控制台记录来自父母和孩子的数据...
问题是当我尝试v-for 子组件中的属性时,它根本不返回任何内容:
/////////////// this does not render anything ///////////////
<template>
<div>
<div v-for="(value, key) in attributes">
{{ key }}
</div>
</div>
</template>
但是,如果我将产品和属性变量都传递给子组件,并在子组件中渲染产品,属性将开始工作!
/////////////// this works /////////////
<template>
<div>
<div v-for="(value, key) in attributes">
{{ key }}
</div>
{{ products }}
</div>
</template>
这到底是怎么回事?
您需要查看我的父方法吗?
【问题讨论】:
-
尝试制作属性计算属性并将其支撑给孩子们?
-
你能把
attributes转储到你的模板中看看发生了什么
标签: vue.js vuejs2 vue-component