0

I am trying to get the event from the @input.native attribute of a el-input tag. Here the template code:

<el-input :value="filter.name" @input.native="updateFilter"></el-input>

And the script code:

updateFilter (e) {
  console.log(e.target.value)
}

My filter.name has been initialized with value "aaa", then I type "b" in the field. For some reason, the output on the log is "aaa" but I need the "aaab" value instead.

Also I can't use @input because it return only the value, I need other attributes too.

Are there anyway to get the valid native input event?

@Update: I am using Vuex so v-model is not an option

7
  • This link might help vuejs.org/v2/guide/… Commented Mar 6, 2019 at 4:52
  • Normally you can use v-model but if you want to pass events then above link might help Commented Mar 6, 2019 at 5:19
  • I didn't want to add a lot of detail but I am using Vuex here so v-model is not an option. Commented Mar 6, 2019 at 7:01
  • Have you gone through the above link which i mentioned which tells how to listen to events using native input? Commented Mar 6, 2019 at 7:48
  • I've tried the link and it not working at all, or should I say, it only return the value rather than the event. I quite sure that your solution definitely working with other UI framework, but this stupid element-ui is something else. Commented Mar 6, 2019 at 7:50

3 Answers 3

2

let's just do v-model with Vuex, and it is very simple :

export default : {
...
computed : {
   filter : {
    get () { return this.$store.state.filter; };
    set (val) { this.$store.commit("setFilter", val);
  }
}
...
}

And then v-model onto filter will be magical.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use computed method. Take one temporary variable and add that variable as v-model to your input. Whenever value is changing assign that variable to vuex store variable(nothing but string concatenation). You can use setters and getters in computed.

Following link might help.

assigning value to vuex store variable using computed setters and getters

Comments

0

I believe you are doing everything right. However, the value can't get updated unless you bind the model (using v-model="filter.name") instead of doing :value.

Here is what I did:

HTML

<el-input 
  class="small" 
  controls-position="right" 
  :value="someValue" 
  @input.native="someFunction">
</el-input>

Script

<script>
    export default {
        name: "CustomizeSmtCampaign",
        data: function () {
            return {
                someValue: 'test'
            }
        },
        methods: {
            someFunction: function (val = '1') {
                console.log('Event Value', val.target.value, '  some value: ', this.blankValue);
            }
        }
    }
</script>

Output

This is the output I got on console as I typed

Event Value teste   some value:  test 
Event Value tester   some value:  test 
Event Value testere   some value:  test 
Event Value testerer   some value:  test 
Event Value testerere   some value:  test 

So your code must be working.

What is wrong, then?

What's wrong is that you are binding to the value, not to the model.

When I changed the :value="someValue" to v-model="someValue", the following was the output:

v-model Output

Event Value teste   some value:  teste 
Event Value tester   some value:  tester 
Event Value testere   some value:  testere 
Event Value testeree   some value:  testeree 
Event Value testereer   some value:  testereer 
Event Value testereere   some value:  testereere

Summary

Always bind the value using v-model (not using :value). That's how Vue achieves the reactiveness!

Hope that helped.

2 Comments

I am using Vuex here, so binding with v-model is not an option
why not? You can do data: function () { return { someValue: this.$store.getters.someValue } } in the component you need the value.

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.