【发布时间】:2021-04-10 19:01:27
【问题描述】:
目前我们有一个文本输入,其值存储在我们的 vuex 存储中,作为 props 传入并通过computed property 检索。在同一个 vuex 存储中,我们有一个选择范围(两个数字的数组),这将允许选择/突出显示输入中的文本。
这是代码的简化版本:
<template>
<input type="text" v-model="value" @select="selectionHandler" />
</template>
<script>
export default {
props: ['props'],
computed: {
value: function() {
return this.props.value // some string
},
selectionRange: {
get: function () { return this.props.selectionRange }, // [number, number]
set: function (range) { /* dispatch store action to update, which will update props */ }
}
},
methods: {
selectionHandler(e) {
this.selectionRange = [e.currentTarget.selectionStart, e.currentTarget.selectionEnd]
}
}
}
</script>
目标是选择/突出显示文本(来自商店)并根据用户选择的内容更新商店。我打算使用setSelectionRange (docs)。我想我可以使用refs 和watchers,但这似乎有点矫枉过正。
【问题讨论】:
标签: javascript vue.js