今天遇到个比较奇怪的问题,导致了死循环,请教了一系列大佬之后最终判断为 vue 视图重绘了。 记录一下,也想请求一下各位大佬更好的解决方案, 这里感谢各位了。
贴上代码, 如果需要执行请注释
<html>
<head>
<title>测试</title>
<link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div id="app">
<div class="alert alert-primary" v-for="item in datas">
<ul v-for="month in item.months">
<li>{{month.month}} - {{month.name}}</li>
<!-- 循环方法 -->
<li>{{lastMonthStaff(item)}}</li>
</ul>
</div>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
datas: [
{
title: 2018,
months: [
{ month: 1, name: '12asds' },
{ month: 2, name: 'adsfsdf' },
{ month: 3, name: 'zcvzcv' },
{ month: 4, name: 'zvcc' },
{ month: 5, name: 'eqr124' },
{ month: 6, name: 'asdfsd' },
{ month: 7, name: 'dsafds' },
{ month: 8, name: 'adsfsdfv' },
{ month: 9, name: 'yiyu' },
{ month: 10, name: 'cxvcxv' },
]
},
{
title: 2019,
months: [
{ month: 1, name: 'uteye' },
{ month: 2, name: 'dfgh' },
{ month: 3, name: 'AF' },
{ month: 4, name: 'FAGSGH' },
{ month: 5, name: 'DSGS' },
{ month: 6, name: '' },
{ month: 7, name: 'SFDG' },
{ month: 8, name: 'DSFG' },
{ month: 9, name: 'SDFG' },
{ month: 10, name: 'FDHDG' },
]
},
{
title: 2020,
months: [
{ month: 1, name: 'FGHGF' },
{ month: 2, name: 'FGH' },
{ month: 3, name: 'FDH' },
{ month: 4, name: 'FGHCVN' },
{ month: 5, name: 'BVCN' },
{ month: 6, name: '' },
{ month: 7, name: 'UIY' },
{ month: 8, name: 'DSHFM' },
{ month: 9, name: 'FSD' },
{ month: 10, name: 'DFSFDG' },
]
},
],
allotUsers: []
},
methods: {
isShow(val) {
switch (val) {
case undefined:
return false
case '':
return false
case null:
return false
default:
return true
}
},
lastMonthStaff (row) {
for (var obj of row.months) {
if (obj.month === new Date().getMonth()) {
if (this.isShow(obj.name)) {
// 触发方法
this.setAllotUsers(obj.name)
// 输出上月 name
return obj.name
} else {
return '无'
}
}
}
},
setAllotUsers(val) {
console.log(val)
// 会触发视图更新导致死循环
this.allotUser.push(val)
// 想的笨办法解决方案
this.allotUsers[this.allotUsers.length] = val
},
}
})
</script>
</body>
</html>
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.