So when I click the internal button, the increaseCount() method is bound to its click event so it gets called. There is no way to bind the event to the external button, whose click event I am listening for with jQuery, so I'll need some other way to call increaseCount. It seems this works: vm.$children[0].increaseCount(); However, this is not a good solution because I am referencing the component by its index in the children array, and with many components this is unlikely to stay constant and the code is less readable.","url":"https://ednsquare.com/question/how-to-call-a-vue-js-component-method-from-outside-the-component-------TZP3JV","mainEntityOfPage":"https://ednsquare.com/question/how-to-call-a-vue-js-component-method-from-outside-the-component-------TZP3JV","image":{"@type":"ImageObject","url":"https://cdn.ednsquare.com/s/*/d8e61c58-683f-4521-8ed9-7b873747514b.png"},"answerCount":2,"upvoteCount":4,"dateCreated":"2019-07-06T12:10:48.000Z","author":{"@type":"Person","name":"Prasad Patil","image":"https://cdn.ednsquare.com/s/*/f89a1717-2322-4d93-87a4-aba26711217e.jpeg","url":"https://ednsquare.com/@prasad"},"acceptedAnswer":{"@type":"Answer","text":"Using Vue's ref directive. This allows a component to be referenced from the parent for direct access. E.g. Have a compenent registered on my parent instance: var vm = new Vue({ el: '#app', components: { 'my-component': myComponent } }); Render the component in template/html with a reference: Now, elsewhere I can access the component externally See this fiddle for an example: old example using Vue 1:","dateCreated":"2019-07-06T12:18:17.000Z","upvoteCount":3,"url":"https://ednsquare.com/question/thread/6d22cd01-c9e7-4c69-950e-7f27e1b8c5d2","author":{"@type":"Person","name":"Aditya Agrawal","image":"https://cdn.ednsquare.com/s/*/212a3510-ddb5-4aa4-8953-fb98c306dc6c.jpeg","url":"https://ednsquare.com/@aditya"}},"suggestedAnswer":[{"@type":"Answer","text":"Since Vue2 this applies: var bus = new Vue() // in component A's method bus.$emit('id-selected', 1) // in component B's created hook bus.$on('id-selected', function (id) { // ... }) See here for the Vue docs. And here is more detail on how to set up this event bus exactly. If you'd like more info on when to use properties, events and/ or centralized state management see this article.","dateCreated":"2019-07-06T12:24:26.000Z","upvoteCount":3,"url":"https://ednsquare.com/question/thread/992d440a-6c77-48fa-80fe-a7662da3b762","author":{"@type":"Person","name":"Chris Gregori","image":"https://cdn.ednsquare.com/s/*/52236c40-87e8-4b83-b0cc-af13d9104df7.jpeg","url":"https://ednsquare.com/@chrisgreg"}}]}}Ednsquare

How to call a Vue.js component method from outside the component?

Let's say I have a main Vue instance that has child components. Is there a way of calling a method belonging to one of these components from outside the Vue instance entirely?

Here is an example:
var vm = new Vue({ el: '#app', components: { 'my-component': { template: '#my-template', data: function() { return { count: 1, }; }, methods: { increaseCount: function() { this.count++; } } }, } }); $('#external-button').click(function() { vm['my-component'].increaseCount(); // This doesn't work });
<script src="http://vuejs.org/js/vue.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="app"> <my-component></my-component> <br> <button id="external-button">External Button</button> </div> <template id="my-template"> <div style="border: 1px solid; padding: 5px;"> <p>A counter: {{ count }}</p> <button @click="increaseCount">Internal Button</button> </div> </template>

So when I click the internal button, the increaseCount() method is bound to its click event so it gets called. There is no way to bind the event to the external button, whose click event I am listening for with jQuery, so I'll need some other way to call increaseCount.

It seems this works:
vm.$children[0].increaseCount();

However, this is not a good solution because I am referencing the component by its index in the children array, and with many components this is unlikely to stay constant and the code is less readable.

Write your answer...

Never miss a post from Prasad Patil, when you sign up for Ednsquare.