I'm working with the popular vue-select component for a VueJs project. I want to customize a keyDownEvent and looked into the docs to see how to achieve this. I was met with a cryptic example using a combination of modern JS techniques.
<template>
<v-select
taggable
multiple
no-drop
:map-keydown="handlers"
placeholder="enter an email"
/>
</template>
<script>
export default {
name: 'CustomHandlers',
data: {
variableIwantAccessTo: []
},
methods: {
handlers: (map, vm) => ({
...map, 50: e => {
e.preventDefault();
if( e.key === '@' && vm.search.length > 0 ) {
vm.search = `${vm.search}@gmail.com`;
}
},
}),
},
};
</script>
I understand the simple logic within the if block, but I do not understand the combination of techniques used that leads up to it. The main issue I'm having with my custom implementation is losing the context for the this object. I want something like this:
handlers: (map, vm) => ({
...map, 50: e => {
e.preventDefault();
if( e.key === '@' && vm.search.length > 0 ) {
console.log(this.variableIwantAccessTo);
}
},
I cannot figure out how to pass it down with this implementation. Here's a ref to the docs: https://vue-select.org/guide/keydown.html#mapkeydown
vmis not the Vue instance as I thought. You'd have to changehandler: (map, vm) => ({/*...*/})tohandler(map, vm) { return {/*...*/} }, where you'd be able to usethisas the Vue instance.