【发布时间】:2021-11-12 11:17:20
【问题描述】:
伙计们!最近在做一个 Vue.js 项目,想尽量拆分成小组件。有一个组件从父组件呈现列表,我想从道具更改某些操作的一些属性。在这一点上,我只是好奇 props 变量是否在 Vue.js 组件中是可变的。
下面是一个示例代码以便更好地理解:
<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 // Should this work or not?
},
removeOption(index) {
this.options.splice(index, 1)
},
addOption() {
this.options.push({
answer: '',
correct: false,
})
},
},
}
</script>
感谢任何评论。
【问题讨论】:
标签: javascript vue.js mutable vue-props