我使用 VueJS(在 Laravel 中)使用变量的值执行简单循环。
在这里,值来自模板 Prop 。
HTML:
<template id="segment" t_nb=2></template>
<div id="test_loop">
<b>@{{ t_nb }}</b>
<div v-for="a in t_nb">
<seg>@{{ a }}</seg>
</div>
</div>
VueJS:
Vue.component(
'seg', {
template: '#segment',
props: ['t_nb']
}
);
输出:
<!-- (Empty)-->
在这里,我无法循环两次。
如果我以直接方式将“t_nb”的值传递为 2,
<div v-for="a in 2">
<seg>@{{ a }}</seg>
</div>
然后,我得到输出为,
1
2
但是我在两者上传递了相同的值,vuejs 只接受直接赋值。
我的代码有什么问题吗?
这怎么可能或有其他解决方案吗?
最佳答案
尝试将您的t_nb props指定为数组,例如:
props: {
't_nb': {
type: Array,
default: [1, 2], // Value that you want to loop
}
}
然后,如果您想为 t_nb 属性分配新值,请执行以下操作:
<seg :t_nb="[3,4,5,6]"></seg>
关于javascript - 为什么我不能在 VueJS 的 for 循环中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41094571/