[英]Vue js v-for not looping thru component property
I have the following setup: 我有以下设置:
JS JS
var session = {
content : {
products : {
options : {
page : 1,
productsPerPage : 20,
orderBy : 'customPriority',
orderDirection : 'asc'
},
productList : [
{name: 'a'},
{name: 'b'},
{name: 'c'},
{name: 'd'},
]
}
}
}
Vue.component('product-list', {
props: {
options: { type: Object },
products : { type: Array, required: true }
}
})
var app = new Vue({
el: '#app',
data: session
})
HTML 的HTML
<div id="app">
<product-list
:options="content.products.options"
:products="content.products.productList"
>
<div
v-for="product in products"
>
<h1>
{{product.name}}
</h1>
</div>
</product-list>
</div>
(jsFiddle: https://jsfiddle.net/Lfw94bgz/1/ ) (jsFiddle: https ://jsfiddle.net/Lfw94bgz/1/)
My problem is that the v-for directive seems to be not working, the products array is defined and shows up in vue-dev-tools, I've built this example seems to be doing the same (iterating thru an array passed to the compent via prop). 我的问题是v-for指令似乎无法正常工作,products数组已定义并显示在vue-dev-tools中,我建立的这个示例似乎在做同样的事情(通过将数组传递给通过道具获得权限)。
No errors/warning in the console, I'm quite lost, thanks for your help 控制台中没有错误/警告,我很迷路,感谢您的帮助
I played around with it a bit and got it working. 我试了一下,让它正常工作。 Here is a working fork of your project .
这是您项目的工作分支 。 The changes I made are in the HTML section, where the
<div id="listTemplate">
is, and I added template: '#listTemplate'
to the product-list component declaration 我所做的更改位于HTML部分中的
<div id="listTemplate">
所在的位置,并且我向产品列表组件声明中添加了template: '#listTemplate'
I think you just have a little bit of a misunderstanding about what a Vue component is. 我认为您对Vue组件是什么有点误解。 A Vue component is a reusable set of JavaScript and HTML code that you can simply inject into anywhere you want.
Vue组件是一组可重用的JavaScript和HTML代码,您可以将它们简单地注入到所需的任何位置。 You need to define what a component looks like inside a template, and THEN you can just simply say
<product-list></product-list>
from within your App Vue instance. 您需要定义组件在模板中的外观,然后您只需在App Vue实例中简单地说
<product-list></product-list>
。
What the template does is just define what HTML code is going to be placed wherever the component tags are. 模板所做的只是定义将在组件标签所在的位置放置哪些HTML代码。 For your example, think of
<product-list></product-list>
as just saying "copy and paste the contents of <div id="listTemplate">
right here" 以您的示例为例,将
<product-list></product-list>
视为<div id="listTemplate">
在此处复制并粘贴<div id="listTemplate">
的内容”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.