4

I have a vue component as

var app_3 = new Vue({
    el:'#app-3',
    data() {
        return {
            step:1,
            models:''
        }
    }
});

And I am handling a click event using jquery like

$('body').on('click', '.models', function() {
});

I would like to access the vue data models from the jquery event handler. How can I access it?

5
  • Can you please add more script and explain why do you want to access vue stuff in jquery? Commented Nov 12, 2018 at 5:44
  • You're almost 100% doing it wrong. Let Vue handle the click, not jQuery. Commented Nov 12, 2018 at 5:56
  • Agreed it's probably not the best way to do things. But you could save your root instance to a global window variable, like this person has: rent-a-hero.de/wp/2017/08/25/… Commented Nov 12, 2018 at 5:58
  • It better would be to add the handler from within vue, probably from within the mounted lifecycle hook Commented Nov 12, 2018 at 6:00
  • @JamesWesc Why would he not simply add a v-on:click directive to his .models element? Commented Nov 12, 2018 at 6:05

3 Answers 3

4

I dont know your purpose but you can use app_3.step to get and set vue data.

var app_3 = new Vue({
    el:'#app-3',
    data() {
        return {
            step:1,
            models:''
        }
    }
});

$('body').on('click', '.models', function() {
app_3.step +=1;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<body>
<div id="app-3">
  Step : {{step}}
</div>
<button class="models">Models</button>
</body>

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

1 Comment

This works, but it is not the best way. See connexo's answer for the correct way to handle events in Vue.
1

Don't mangle jQuery and Vue together. For event handling Vue has all the tools necessary.

var app_3 = new Vue({
    el:'#app-3',
    data() {
        return {
            step: 1,
            models:''
        }
    },
    methods: {
      decreaseStep() {
        this.step -= 1
      }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app-3">
  Step : {{step}}
  <button @click="step+=1">+</button>
  <button @click="decreaseStep">-</button>
</div>

These are just two simple examples. You'd go with the +-button code if the task is so trivial, otherwise you'd create a function inside the methods object on your viewmodel.

Comments

1

On Vue Js i create a method and called it in before Mount:

 var app_3 = new Vue({
        el:'#app-3',
        data() {
            return {
                step:1,
                models:''
            }
        },
        methods: {
            // called by beforeMount()
            foo() {
                let proxy = this
                $('body').on('click', '.models', function() {
                    // call a method
                    proxy.bar()
                    // set a variable data
                    proxy.step = 2
                });
            },
            // called each .model click
            bar(){
                console.log('it id working');
            }
        },
        beforeMount() {
            this.foo()
        }
    })

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.