当使用对象数组时,在v-for循环中是否可以同时将当前对象赋值给一个变量并解构其属性?像这样:
<div v-for="(person = {name, age}, index) in persons">
最终我希望能够在模板中同时使用整个person对象和它的属性。
Your Answer
1 个回答
据我所知,你不能同时做这两件事。
但是,你可以解构,例如:
<div v-for="({name, age}, index) in persons">
然后你可以通过索引访问正确的元素:persons[index]。
示例:
new Vue({
el: "#app",
data: {
todos: [{
text: "Learn JavaScript",
done: false
},
{
text: "Learn Vue",
done: false
},
{
text: "Play around in JSFiddle",
done: true
},
{
text: "Build something awesome",
done: true
}
]
},
methods: {
toggle: function(index) {
this.todos[index].done = !this.todos[index].done
}
}
})
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<h2>Todos:</h2>
<ol>
<li v-for="({text, done}, i) in todos">
<label>
<input type="checkbox"
v-on:change="toggle(i)"
v-bind:checked="done">
<del v-if="done">
{{ text }}
</del>
<span v-else>
{{ text }}
</span>
{{todos[i]}}
</label>
</li>
</ol>
</div>
Hot Questions
function_exists()无法判定自定义函数
2024-04-29 11:01:01
google 浏览器 手机版显示的怎么实现
2024-04-23 00:22:19
子窗口操作父窗口,输出没反应
2024-04-19 15:37:47
父窗口没有输出
2024-04-18 23:52:34
关于CSS思维导图的课件在哪?
2024-04-16 10:10:18
Hot Tools
vc9-vc14(32+64位)运行库合集(链接在下方)
phpStudy安装所需运行库集合下载
VC9 32位
VC9 32位 phpstudy集成安装环境运行库
php程序员工具箱完整版
程序员工具箱 v1.0 php集成环境
VC11 32位
VC11 32位 phpstudy集成安装环境运行库
SublimeText3汉化版
中文版,非常好用
热门话题
抖音等级价目表1-75
20335
7
20335
7
wifi显示无ip分配
13530
4
13530
4
虚拟手机号接收验证码
11850
4
11850
4
gmail邮箱登陆入口在哪里
8835
17
8835
17
windows安全中心怎么关闭
8420
7
8420
7
热门文章
2025年加密货币市场十大趋势预测:下一个风口在哪里?
2025-11-07
By DDD
币圈土狗项目如何识别?避免归零币的陷阱与风险预警
2025-11-07
By DDD
铁路12306支付失败订单还在吗_铁路12306支付失败订单处理方法
2025-11-07
By DDD
win10字体安装后在软件里找不到怎么办_win10字体安装与识别方法
2025-11-07
By DDD
高效处理PHP表单中动态数量的问答数据更新
2025-11-07
By DDD





