5

Сan't get context from directive

<template lang="pug">
    div(class="form")
        select(name="name" v-model="form.input" v-select="form.inputs")
            option(v-for="(option, key) in data" :value="key")
                | {{values[key].value}}
</template>

<script>
    export default {
        data() {
            return {
                data: []
            }
        },
        directives: {
            select: {
                mounted: function(el, binding, vnode) {
                    console.log(vnode.context); // return undefined
                }
            }
        }
    }
</script>

vnode.context - return undefined

vnode.appContext - return null

1 Answer 1

2

As mentioned in the migration guide:

In Vue 2, the component instance had to be accessed through the vnode argument:

bind(el, binding, vnode) {   
    const vm = vnode.context 
} 

In Vue 3, the instance is now part of the binding:

mounted(el, binding, vnode) {   
  const vm = binding.instance
}

Therefore:

<script>
    export default {
        data() {
            return {
                data: []
            }
        },
        directives: {
            select: {
                mounted: function(el, binding) {
                    console.log(binding.instance);
                }
            }
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.