简体   繁体   English

Vue:为什么在商店中声明时计算属性需要 v-model 指令中的 .value 而不是在组件中声明时

[英]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.

相关问题 为什么 vuejs 在计算属性中引用 v-model 时会复制其 v-model 数据? - Why does vuejs replicates its v-model data when the v-model is referenced within a computed property? 为什么当列表更改时,v-for(使用计算列表)内的组件内的 v-model 会发生奇怪的变化? - Why is v-model inside a component inside a v-for (using a computed list) changing weirdly when the list changes? 自定义 Vue select 组件在 v-model 值更改时不更新所选选项 - Custom Vue select component not updating selected option when v-model value changed 为什么 v-model 在 vue.extend 中使用时不起作用? - Why v-model not working when used in vue.extend? 获取 object 绑定 v-model 时 vue3 计算集不起作用 - vue3 computed set doesn't work when get an object binding v-model 如何使用字符串作为 v-model 指令中的属性名称来访问 Vue 组件的数据属性? - How can I access my Vue component's data property using a string as it's property name in a v-model directive? v-model 到计算属性的动态绑定 - Dynamic binding of v-model to computed property 获取子组件Vue中的v-model值 - Get v-model value in child component Vue 自定义 Vue 组件上的 v-model 不会更改输入值 - v-model on custom Vue component not changing value on input 当 v-model 改变时更新一个值 - Updating a value when v-model changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM