admin管理员组文章数量:1022956
In Vue.js 3 (beta), I have defined an array using reactive
, as I want to bind its contents to some UI controls in a loop. So far, this works and everything is fine.
Now, I need to update a value within this array, which means I need to run a find
or a findIndex
on this array. Since the array is being proxied by Vue.js, this doesn't work as expected: The proxy is just not a simple plain old array.
What I did is get a copy using toRaw
, run findIndex
on that one, and then update the original array using the index. This works, but of course it doesn't seem to be very elegant.
Is there a better way to approach this?
PS: It's fine if it's a solution that only works in Vue 3, I don't care about the 2.x series.
In Vue.js 3 (beta), I have defined an array using reactive
, as I want to bind its contents to some UI controls in a loop. So far, this works and everything is fine.
Now, I need to update a value within this array, which means I need to run a find
or a findIndex
on this array. Since the array is being proxied by Vue.js, this doesn't work as expected: The proxy is just not a simple plain old array.
What I did is get a copy using toRaw
, run findIndex
on that one, and then update the original array using the index. This works, but of course it doesn't seem to be very elegant.
Is there a better way to approach this?
PS: It's fine if it's a solution that only works in Vue 3, I don't care about the 2.x series.
Share Improve this question edited Aug 17, 2020 at 2:56 tony19 139k23 gold badges278 silver badges348 bronze badges asked Aug 16, 2020 at 20:50 Golo RodenGolo Roden 151k102 gold badges315 silver badges444 bronze badges1 Answer
Reset to default 6All the array's methods are still accessible through the Proxy
, so you can still use find
or findIndex
on it:
import { reactive } from 'vue'
const items = reactive([1,2,3])
console.log(items.find(x => x % 2 === 0))
console.log(items.findIndex(x => x % 2 === 0))
const MyApp = {
setup() {
const items = Vue.reactive([1,2,3])
return {
items,
addItem() {
items.push(items.length + 1)
},
logFirstEvenValue() {
console.log(items.find(x => x % 2 === 0))
},
logFirstEvenIndex() {
console.log(items.findIndex(x => x % 2 === 0))
},
incrementItems() {
for (let i = 0; i < items.length; i++) {
items[i]++
}
}
}
}
}
Vue.createApp(MyApp).mount('#app')
<script src="https://unpkg./[email protected]"></script>
<div id="app">
<button @click="logFirstEvenValue">Log first even item</button>
<button @click="logFirstEvenIndex">Log index of first even item</button>
<button @click="incrementItems">Increment items</button>
<button @click="addItem">Add item</button>
<ul>
<li v-for="item in items">{{item}}</li>
</ul>
</div>
In Vue.js 3 (beta), I have defined an array using reactive
, as I want to bind its contents to some UI controls in a loop. So far, this works and everything is fine.
Now, I need to update a value within this array, which means I need to run a find
or a findIndex
on this array. Since the array is being proxied by Vue.js, this doesn't work as expected: The proxy is just not a simple plain old array.
What I did is get a copy using toRaw
, run findIndex
on that one, and then update the original array using the index. This works, but of course it doesn't seem to be very elegant.
Is there a better way to approach this?
PS: It's fine if it's a solution that only works in Vue 3, I don't care about the 2.x series.
In Vue.js 3 (beta), I have defined an array using reactive
, as I want to bind its contents to some UI controls in a loop. So far, this works and everything is fine.
Now, I need to update a value within this array, which means I need to run a find
or a findIndex
on this array. Since the array is being proxied by Vue.js, this doesn't work as expected: The proxy is just not a simple plain old array.
What I did is get a copy using toRaw
, run findIndex
on that one, and then update the original array using the index. This works, but of course it doesn't seem to be very elegant.
Is there a better way to approach this?
PS: It's fine if it's a solution that only works in Vue 3, I don't care about the 2.x series.
Share Improve this question edited Aug 17, 2020 at 2:56 tony19 139k23 gold badges278 silver badges348 bronze badges asked Aug 16, 2020 at 20:50 Golo RodenGolo Roden 151k102 gold badges315 silver badges444 bronze badges1 Answer
Reset to default 6All the array's methods are still accessible through the Proxy
, so you can still use find
or findIndex
on it:
import { reactive } from 'vue'
const items = reactive([1,2,3])
console.log(items.find(x => x % 2 === 0))
console.log(items.findIndex(x => x % 2 === 0))
const MyApp = {
setup() {
const items = Vue.reactive([1,2,3])
return {
items,
addItem() {
items.push(items.length + 1)
},
logFirstEvenValue() {
console.log(items.find(x => x % 2 === 0))
},
logFirstEvenIndex() {
console.log(items.findIndex(x => x % 2 === 0))
},
incrementItems() {
for (let i = 0; i < items.length; i++) {
items[i]++
}
}
}
}
}
Vue.createApp(MyApp).mount('#app')
<script src="https://unpkg./[email protected]"></script>
<div id="app">
<button @click="logFirstEvenValue">Log first even item</button>
<button @click="logFirstEvenIndex">Log index of first even item</button>
<button @click="incrementItems">Increment items</button>
<button @click="addItem">Add item</button>
<ul>
<li v-for="item in items">{{item}}</li>
</ul>
</div>
本文标签: javascriptSearching a reactive array in Vuejs 3Stack Overflow
版权声明:本文标题:javascript - Searching a reactive array in Vue.js 3 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745576817a2157070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论