Hello everyone! I've been diving into a Vue.js project recently and my goal is to break it down into smaller components whenever possible. One of the components in question is responsible for rendering a list from the parent component, and I'm interested in whether or not the props variable is mutable within a Vue.js component.
Below is an example code snippet to provide context:
<template>
<div>
<div
class="d-flex align-items-center mb-2 justify-between"
v-for="(option, index) in options"
:key="index"
>
<b-input-group>
<b-input-group-prepend>
<b-button variant="secondary">
<b-icon icon="grip-horizontal"></b-icon>
</b-button>
</b-input-group-prepend>
<b-form-input
placeholder="Option"
@input="updateOption(index, $event)"
:value="option.answer"
></b-form-input>
<b-input-group-append>
<b-button variant="secondary" @click="removeOption(index)">
Remove
</b-button>
</b-input-group-append>
</b-input-group>
<b-form-checkbox
:id="`option-${index}`"
name="options"
class="f-14 text-muted ml-1"
v-model="option.correct"
>
Correct?
</b-form-checkbox>
</div>
</div>
</template>
<script>
export default {
name: 'multi-options',
props: ['options'],
methods: {
updateOption(index, value) {
this.options[index].answer = value // I wonder if this will work
},
removeOption(index) {
this.options.splice(index, 1)
},
addOption() {
this.options.push({
answer: '',
correct: false,
})
},
},
}
</script>
Your thoughts and feedback are much appreciated.