[英]Vue: Why does computed property require .value in v-model directive when declared in a store but not when declared in the component
When I move a computed prop from my component to the store, I have to use .value in the v-model directive.当我将计算道具从我的组件移动到商店时,我必须在 v-model 指令中使用 .value 。
The two quasar select lists below are both working.下面的两个类星体选择列表都有效。 They both display the state from the store, with the first one accessing the state from a computed property in the store and the second one accessing the state from a computed property in the component.
它们都显示存储中的状态,第一个从存储中的计算属性访问状态,第二个从组件中的计算属性访问状态。
Both computed props are essentially the same implementation, why do I have to use .value in the first one's v-model directive?两个计算道具本质上是相同的实现,为什么我必须在第一个的 v-model 指令中使用.value ?
<q-select
outlined
v-model="store.warehouse.currentDepotCode.value"
:options="store.warehouse.getDepotSelectList()"
emit-value
map-options
label="Select a Depot"
class="q-ma-md"
/>
<q-select
outlined
v-model="currentDepotCode"
:options="store.warehouse.getDepotSelectList()"
emit-value
map-options
label="Select a Depot"
class="q-ma-md"
/>
setup() {
const store = inject("store");
const currentDepotCode = computed({
get(){
return store.warehouse.state.currentDepot;
},
set(depotCode){
store.warehouse.setCurrentDepot(depotCode);
}
});
return {
store,
currentDepotCode,
};
store/index.js商店/index.js
const state = reactive({
depots,
currentDepot: 'VIC',
});
const currentDepotCode = computed({
get(){
return state.currentDepot;
},
set(depotCode){
state.currentDepot = depotCode;
}
});
export default {
state: readonly(state),
...methods,
...getters,
currentDepotCode,
};
(I am using a computed prop because the select component will appear on a number of pages so I want to use a setter function and I dont want to repeat the computed prop on every page, so its going in the store. Happy to have comments on this set up too). (我使用的是计算道具,因为选择组件会出现在许多页面上,所以我想使用 setter 函数,我不想在每个页面上重复计算道具,所以它在商店里。很高兴有评论在这个设置上)。
i think because q-select
options is an array of objects, store.warehouse.getDepotSelectList()
the real value of each element我认为因为
q-select
选项是一个对象数组, store.warehouse.getDepotSelectList()
是每个元素的真实值
ex: [{ key: 1, value: 'first' }, { key: 2, value: 'second' }]
例如:
[{ key: 1, value: 'first' }, { key: 2, value: 'second' }]
because in documentation it's using value directly https://quasar.dev/vue-components/select因为在文档中它直接使用值https://quasar.dev/vue-components/select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.