如何将PINIA商店与V-For指令使用,同时保持其反应性?
我有一个类似于以下代码段的PINIA数据存储,该代码段存储了用户信息和他下达的个别订单列表:
order.js
import { defineStore } from 'pinia'
import { reactive } from 'vue'
export const useOrderStore = defineStore('order', {
state: () => ({
username: '',
orders: reactive([
{
id: '',
item: '',
price: ''
}
])
}),
})
我也使用V-For指令来渲染该组件应显示单个订单
ordersComp.vue
<template>
<div class="orders_container">
<div v-for="(order, index) in orders" :key="order.id">
<OrderComp />
</div>
</div>
</template>
<script>
import { storeToRefs } from 'pinia'
import { useOrderStore } from "@/store/order";
setup() {
const { orders } = storeToRefs(useOrderStore())
return { orders };
},
</script>
我如何访问儿童组件ordorcomp中个体订单的商店数据,
我基本上想要这样的东西: ordercomp.vue
<div>
<p>{{ orders.id }}</p>
<input v-model="orders.item" />
<input v-model="orders.price" />
</div>
<script>
import { storeToRefs } from 'pinia'
import { useOrderStore } from "@/store/order";
setup() {
const { orders } = storeToRefs(useOrderStore())
return { orders };
},
</script>
仍然保持其反应性状态?儿童组件如何知道要修改的订单数组的哪个顺序?我应该/应该将PINIA数据存储与将数据从父母传递到子女的道具相结合吗? (尽管对我来说这似乎有些不对劲,因为Pinia可能能够替换组件之间的所有数据),并且由于项目和价格与输入字段界约定,因此它们当然应该根据用户输入动态更改。
I have a pinia data store similar to the following code snippet that stores user information and a list of individual orders he is placing:
order.js
import { defineStore } from 'pinia'
import { reactive } from 'vue'
export const useOrderStore = defineStore('order', {
state: () => ({
username: '',
orders: reactive([
{
id: '',
item: '',
price: ''
}
])
}),
})
Also I am using the v-for directive to render the components that should display the individual orders
OrdersComp.vue
<template>
<div class="orders_container">
<div v-for="(order, index) in orders" :key="order.id">
<OrderComp />
</div>
</div>
</template>
<script>
import { storeToRefs } from 'pinia'
import { useOrderStore } from "@/store/order";
setup() {
const { orders } = storeToRefs(useOrderStore())
return { orders };
},
</script>
How can I access the store data for the individual orders in the child component OrderComp
Basically I want something like this:
OrderComp.vue
<div>
<p>{{ orders.id }}</p>
<input v-model="orders.item" />
<input v-model="orders.price" />
</div>
<script>
import { storeToRefs } from 'pinia'
import { useOrderStore } from "@/store/order";
setup() {
const { orders } = storeToRefs(useOrderStore())
return { orders };
},
</script>
and still keep its reactive state? How does the child component know which order of the orders array to modify? Can/Should I combine the pinia data store with props that pass the data from parent to child? (Though this seems somewhat wrong for me, as pinia is probably able to replace all data passing between components) And furthermore as item and price are bound to input fields, they should of course dynamically change based on a user input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于 estus flasks 评论我通过将事件从child ordorcomp发射到parent ordersComp以及每个更改中,从而使它起作用。它调用了一个函数,该函数将我的订单数组在数据存储中的正确索引中进行了修改。
因此,按照上面的示例,我做了这样的事情:
order.js
orderscomp.vue
注意:我在此处使用包装函数来发出输入,但是您可以当然,EG直接通过 @input/ @canse
ordercomp.vue
modelwrapper.js
Based on Estus Flasks comments I got it working by emitting events from the child OrderComp to the parent OrdersComp and on each change it invoked a function that modified my orders array at the correct index in the datastore.
So following the example above I did something like this:
order.js
OrdersComp.vue
Note: I use a wrapper function here to emit the inputs, however you can of course emit it directly e.g. via @input/@change
OrderComp.vue
modelWrapper.js