I am trying to create a board that can change size at the click of a button. The board has width and height attributes that determine how many cells wide and tall it is. When I change these width and height attributes, the board re-renders to the proper size, but its props (namely a coords prop that is an [x,y] array of the position of the cell on the board) does not update properly.
When the page first renders (with a board width and height of 10) and I console.log the cells of the board, the top-left cell has the coordinates of [0,0], which is correct. screenshot.
However, after resetting the width and height to 5 each, the top-left cell has coords of [4,0], and I don't understand why. screenshot.
Here is my code for App.vue:
<template>
<button @click="resetBoard()">reset game with new board dimensions</button>
<div :style="{ width: boardWidth, height: boardHeight }">
<div v-for="y in height" :key="y">
<BoardCell
v-for="x in width"
ref="cells"
:coords="[x - 1, y - 1]"
:key="y * width + x"
/>
</div>
</div>
<button @click="logCells">log cells to console</button>
</template>
<script>
import BoardCell from "./components/BoardCell.vue";
export default {
name: "App",
components: { BoardCell },
data() {
return {
width: 10,
height: 10,
};
},
computed: {
boardWidth() {
return (this.width * 40).toString() + "px";
},
boardHeight() {
return (this.height * 40).toString() + "px";
},
},
methods: {
resetBoard() {
this.width = 5;
this.height = 5;
},
logCells(){
console.log('this.$refs.cells:')
console.log(this.$refs.cells)
}
},
};
</script>
And here is my code for the cell component:
<template>
<div class="cell">
</div>
</template>
<script>
export default {
props: ['coords']
}
</script>
<style scoped>
.cell {
width: 40px;
height: 40px;
float: left;
line-height: 0px;
background-color: limegreen;
border: 1px green solid;
box-sizing: border-box;
}
</style>