Home > Software engineering >  How to get slot props in computed property?
How to get slot props in computed property?

Time:02-25

I want to get slot props in computed property. Currently I pass the slot props to a method and it work fine. But I want to work with v-model. Can I do that?

// MediaSelect.vue
<template>
    <input-wrapper v-slot="slotProps">
        <gallery-list 
            :value="getValue(slotProps.language)" // current
            v-model="myValue" // I want
        >
        </gallery-list>
    </input-wrapper>
</template>

<script>
    export default {
        computed: function(){
            myValue: {
                set: function(value){
                    this.$emit("input", value);
                },
                get: function(){
                    // I want to get slotProps in here, like this
                    return this.getValue(this.slotProps.language)
                }
                
            }
            
        },
        methods: {
            getValue: function(language){
                ...
            },
        }
    }
</script>

CodePudding user response:

No it's not possible, you cannot pass an argument to a computed property (that's what a method is for).

You should just do this:

<input-wrapper v-slot="slotProps">
  <gallery-list 
    :value="getValue(slotProps.language)"
    @input="$emit('input', $event)"
  >
  </gallery-list>
</input-wrapper>
  • Related