While attempting to use v-model
on an array item's property, I encountered the error message
"[Vue warn]: Error in render function: 'TypeError: Cannot read property 'viewFood' of undefined'
when loading the page. This resulted in a blank page.
This issue is specific to vue.js version 2.x.
https://codepen.io/jzaun/pen/YxYyJN/
HTML:
<div id="ai-config">
<div class="info">
<div class="row">
<h1>Resource Points</h1>
</div>
<div class="row">
<label>Total:</label>
<div class="value">
{{maxResourcePoints}}
</div>
</div>
<div class="row">
<label>Remaining:</label>
<div class="value">
{{maxResourcePoints - usedResourcePoints}}
</div>
</div>
</div>
<div>
<table>
<tr>
<td></td>
<td v-for="(option, idx) in options">
{{option.title}}
</td>
</tr>
<tr v-for="n in directions">
<td>Direction {{n}}</td>
<td v-for="option in options">
<input type="checkbox" v-model="selected[n][option.key]" />
</td>
</tr>
</table>
</div>
</div>
JavaScript:
new Vue ({
el: '#ai-config',
data: {
maxResourcePoints: 10,
usedResourcePoints: 0,
selected: [],
directions: 8,
options: [{
title: 'Food',
key: 'viewFood',
cost: 1
}, {
title: 'Water',
key: 'viewWater',
cost: 1
}, {
title: 'Own',
key: 'viewOwn',
cost: 1
}, {
title: 'Other',
key: 'viewOther',
cost: 1
}]
},
methods: {
},
created: function () {
this.selected = [];
for(i=0; i<8; i++) {
this.selected.push({});
}
}
});