Can't call Vue component method outside of it's element











up vote
0
down vote

favorite












I have a simple vue component with one method that I am trying to call outside of it's wrapper element #app but it is not triggering. Is there a way to reigster the view component so that I could call it with Component.function();



var viewModel = new Vue({
el: "#app",
data: {},
methods: {
test: function() {
alert("test fuction called");
}
}
});


HTML:



<div id="app">

</div>

<a @click="viewModel.test()">Click me!</a>


Fiddle:
https://jsfiddle.net/queeeeenz/Lja7pake/198/










share|improve this question
























  • Why are you using @click on html? Use onclick instead.
    – Abana Clara
    Nov 22 at 7:40






  • 1




    @AbanaClara @click is the short version of v-on:click , its completly valid.
    – Badgy
    Nov 22 at 7:41






  • 1




    @Badgy I know. But the HTML given on the post is apparently an external HTML -- given that OP is accessing a Vue instance via viewModel. This is further made obvious by the component container #app as a sibling of the a element in question
    – Abana Clara
    Nov 22 at 7:43












  • @Badgy I added a fiddle
    – Liga
    Nov 22 at 7:52















up vote
0
down vote

favorite












I have a simple vue component with one method that I am trying to call outside of it's wrapper element #app but it is not triggering. Is there a way to reigster the view component so that I could call it with Component.function();



var viewModel = new Vue({
el: "#app",
data: {},
methods: {
test: function() {
alert("test fuction called");
}
}
});


HTML:



<div id="app">

</div>

<a @click="viewModel.test()">Click me!</a>


Fiddle:
https://jsfiddle.net/queeeeenz/Lja7pake/198/










share|improve this question
























  • Why are you using @click on html? Use onclick instead.
    – Abana Clara
    Nov 22 at 7:40






  • 1




    @AbanaClara @click is the short version of v-on:click , its completly valid.
    – Badgy
    Nov 22 at 7:41






  • 1




    @Badgy I know. But the HTML given on the post is apparently an external HTML -- given that OP is accessing a Vue instance via viewModel. This is further made obvious by the component container #app as a sibling of the a element in question
    – Abana Clara
    Nov 22 at 7:43












  • @Badgy I added a fiddle
    – Liga
    Nov 22 at 7:52













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a simple vue component with one method that I am trying to call outside of it's wrapper element #app but it is not triggering. Is there a way to reigster the view component so that I could call it with Component.function();



var viewModel = new Vue({
el: "#app",
data: {},
methods: {
test: function() {
alert("test fuction called");
}
}
});


HTML:



<div id="app">

</div>

<a @click="viewModel.test()">Click me!</a>


Fiddle:
https://jsfiddle.net/queeeeenz/Lja7pake/198/










share|improve this question















I have a simple vue component with one method that I am trying to call outside of it's wrapper element #app but it is not triggering. Is there a way to reigster the view component so that I could call it with Component.function();



var viewModel = new Vue({
el: "#app",
data: {},
methods: {
test: function() {
alert("test fuction called");
}
}
});


HTML:



<div id="app">

</div>

<a @click="viewModel.test()">Click me!</a>


Fiddle:
https://jsfiddle.net/queeeeenz/Lja7pake/198/







javascript vue.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 7:52

























asked Nov 22 at 7:25









Liga

949




949












  • Why are you using @click on html? Use onclick instead.
    – Abana Clara
    Nov 22 at 7:40






  • 1




    @AbanaClara @click is the short version of v-on:click , its completly valid.
    – Badgy
    Nov 22 at 7:41






  • 1




    @Badgy I know. But the HTML given on the post is apparently an external HTML -- given that OP is accessing a Vue instance via viewModel. This is further made obvious by the component container #app as a sibling of the a element in question
    – Abana Clara
    Nov 22 at 7:43












  • @Badgy I added a fiddle
    – Liga
    Nov 22 at 7:52


















  • Why are you using @click on html? Use onclick instead.
    – Abana Clara
    Nov 22 at 7:40






  • 1




    @AbanaClara @click is the short version of v-on:click , its completly valid.
    – Badgy
    Nov 22 at 7:41






  • 1




    @Badgy I know. But the HTML given on the post is apparently an external HTML -- given that OP is accessing a Vue instance via viewModel. This is further made obvious by the component container #app as a sibling of the a element in question
    – Abana Clara
    Nov 22 at 7:43












  • @Badgy I added a fiddle
    – Liga
    Nov 22 at 7:52
















Why are you using @click on html? Use onclick instead.
– Abana Clara
Nov 22 at 7:40




Why are you using @click on html? Use onclick instead.
– Abana Clara
Nov 22 at 7:40




1




1




@AbanaClara @click is the short version of v-on:click , its completly valid.
– Badgy
Nov 22 at 7:41




@AbanaClara @click is the short version of v-on:click , its completly valid.
– Badgy
Nov 22 at 7:41




1




1




@Badgy I know. But the HTML given on the post is apparently an external HTML -- given that OP is accessing a Vue instance via viewModel. This is further made obvious by the component container #app as a sibling of the a element in question
– Abana Clara
Nov 22 at 7:43






@Badgy I know. But the HTML given on the post is apparently an external HTML -- given that OP is accessing a Vue instance via viewModel. This is further made obvious by the component container #app as a sibling of the a element in question
– Abana Clara
Nov 22 at 7:43














@Badgy I added a fiddle
– Liga
Nov 22 at 7:52




@Badgy I added a fiddle
– Liga
Nov 22 at 7:52












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










I tested for a while.




  1. It might be not able to use @ in elements outside of Vue element

  2. The var viewModel seems not attached to window object


I can run with this though



JS



window.viewModel = new Vue({
el: "#app",
data: {},
methods: {
test: function() {
alert("test fuction called");
}
}
});


HTML



<div id="app">

</div>

<a onClick="viewModel.test()">Click me!</a>





share|improve this answer





















  • Perfect! Thank you!! It worked! That's what I was looking for!
    – Liga
    Nov 22 at 8:51




















up vote
0
down vote













First of all, it seems like you are attaching a click handler the "Vue" way, without actually it being a Vue component. That is not going to work.



To strictly achieve what you want, you have to expose your function to a different scope, e.g. via assigning it to a window attribute.



var viewModel = new Vue({
el: "#app",
data: {},
created () {
// Now it is exposed
window.test = this.test;
},
methods: {
test: function() {
alert("test fuction called");
}
}
});

// And later
window.test();


A better way of doing this is probably by using a global event bus. Instead of exposing random functions in the global scope, you can instead create a bus that you expose instead. The nice part about that is that if everything happens within the Vue application, you could use this.$bus.$emit('...') from anywhere in the Vue application and listen to it everywhere else in the Vue application. The nice part if it is used outside the Vue application is that you use a set interface between the inside of your Vue application and the outside of your Vue application, preventing you from having to expose more and more functions in the global scope, and allowing you to figure out what can and cannot be done from outside the Vue application.



import Vue from 'vue';
export const bus = new Vue();

// Elsewhere
import { bus } from './bus';
Vue.prototype.$bus = bus;

// In outside code
import { bus } from '../../my-vue-application/bus';
bus.$emit('test');

// In your component
var viewModel = new Vue({
el: "#app",
data: {},
created () {
this.$bus.$on('test', this.test);
},
beforeDestroy () {
this.$bus.$off('test', this.test);
},
methods: {
test: function() {
alert("test fuction called");
}
}
});





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425795%2fcant-call-vue-component-method-outside-of-its-element%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    I tested for a while.




    1. It might be not able to use @ in elements outside of Vue element

    2. The var viewModel seems not attached to window object


    I can run with this though



    JS



    window.viewModel = new Vue({
    el: "#app",
    data: {},
    methods: {
    test: function() {
    alert("test fuction called");
    }
    }
    });


    HTML



    <div id="app">

    </div>

    <a onClick="viewModel.test()">Click me!</a>





    share|improve this answer





















    • Perfect! Thank you!! It worked! That's what I was looking for!
      – Liga
      Nov 22 at 8:51

















    up vote
    1
    down vote



    accepted










    I tested for a while.




    1. It might be not able to use @ in elements outside of Vue element

    2. The var viewModel seems not attached to window object


    I can run with this though



    JS



    window.viewModel = new Vue({
    el: "#app",
    data: {},
    methods: {
    test: function() {
    alert("test fuction called");
    }
    }
    });


    HTML



    <div id="app">

    </div>

    <a onClick="viewModel.test()">Click me!</a>





    share|improve this answer





















    • Perfect! Thank you!! It worked! That's what I was looking for!
      – Liga
      Nov 22 at 8:51















    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    I tested for a while.




    1. It might be not able to use @ in elements outside of Vue element

    2. The var viewModel seems not attached to window object


    I can run with this though



    JS



    window.viewModel = new Vue({
    el: "#app",
    data: {},
    methods: {
    test: function() {
    alert("test fuction called");
    }
    }
    });


    HTML



    <div id="app">

    </div>

    <a onClick="viewModel.test()">Click me!</a>





    share|improve this answer












    I tested for a while.




    1. It might be not able to use @ in elements outside of Vue element

    2. The var viewModel seems not attached to window object


    I can run with this though



    JS



    window.viewModel = new Vue({
    el: "#app",
    data: {},
    methods: {
    test: function() {
    alert("test fuction called");
    }
    }
    });


    HTML



    <div id="app">

    </div>

    <a onClick="viewModel.test()">Click me!</a>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 at 8:07









    supl

    895




    895












    • Perfect! Thank you!! It worked! That's what I was looking for!
      – Liga
      Nov 22 at 8:51




















    • Perfect! Thank you!! It worked! That's what I was looking for!
      – Liga
      Nov 22 at 8:51


















    Perfect! Thank you!! It worked! That's what I was looking for!
    – Liga
    Nov 22 at 8:51






    Perfect! Thank you!! It worked! That's what I was looking for!
    – Liga
    Nov 22 at 8:51














    up vote
    0
    down vote













    First of all, it seems like you are attaching a click handler the "Vue" way, without actually it being a Vue component. That is not going to work.



    To strictly achieve what you want, you have to expose your function to a different scope, e.g. via assigning it to a window attribute.



    var viewModel = new Vue({
    el: "#app",
    data: {},
    created () {
    // Now it is exposed
    window.test = this.test;
    },
    methods: {
    test: function() {
    alert("test fuction called");
    }
    }
    });

    // And later
    window.test();


    A better way of doing this is probably by using a global event bus. Instead of exposing random functions in the global scope, you can instead create a bus that you expose instead. The nice part about that is that if everything happens within the Vue application, you could use this.$bus.$emit('...') from anywhere in the Vue application and listen to it everywhere else in the Vue application. The nice part if it is used outside the Vue application is that you use a set interface between the inside of your Vue application and the outside of your Vue application, preventing you from having to expose more and more functions in the global scope, and allowing you to figure out what can and cannot be done from outside the Vue application.



    import Vue from 'vue';
    export const bus = new Vue();

    // Elsewhere
    import { bus } from './bus';
    Vue.prototype.$bus = bus;

    // In outside code
    import { bus } from '../../my-vue-application/bus';
    bus.$emit('test');

    // In your component
    var viewModel = new Vue({
    el: "#app",
    data: {},
    created () {
    this.$bus.$on('test', this.test);
    },
    beforeDestroy () {
    this.$bus.$off('test', this.test);
    },
    methods: {
    test: function() {
    alert("test fuction called");
    }
    }
    });





    share|improve this answer

























      up vote
      0
      down vote













      First of all, it seems like you are attaching a click handler the "Vue" way, without actually it being a Vue component. That is not going to work.



      To strictly achieve what you want, you have to expose your function to a different scope, e.g. via assigning it to a window attribute.



      var viewModel = new Vue({
      el: "#app",
      data: {},
      created () {
      // Now it is exposed
      window.test = this.test;
      },
      methods: {
      test: function() {
      alert("test fuction called");
      }
      }
      });

      // And later
      window.test();


      A better way of doing this is probably by using a global event bus. Instead of exposing random functions in the global scope, you can instead create a bus that you expose instead. The nice part about that is that if everything happens within the Vue application, you could use this.$bus.$emit('...') from anywhere in the Vue application and listen to it everywhere else in the Vue application. The nice part if it is used outside the Vue application is that you use a set interface between the inside of your Vue application and the outside of your Vue application, preventing you from having to expose more and more functions in the global scope, and allowing you to figure out what can and cannot be done from outside the Vue application.



      import Vue from 'vue';
      export const bus = new Vue();

      // Elsewhere
      import { bus } from './bus';
      Vue.prototype.$bus = bus;

      // In outside code
      import { bus } from '../../my-vue-application/bus';
      bus.$emit('test');

      // In your component
      var viewModel = new Vue({
      el: "#app",
      data: {},
      created () {
      this.$bus.$on('test', this.test);
      },
      beforeDestroy () {
      this.$bus.$off('test', this.test);
      },
      methods: {
      test: function() {
      alert("test fuction called");
      }
      }
      });





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        First of all, it seems like you are attaching a click handler the "Vue" way, without actually it being a Vue component. That is not going to work.



        To strictly achieve what you want, you have to expose your function to a different scope, e.g. via assigning it to a window attribute.



        var viewModel = new Vue({
        el: "#app",
        data: {},
        created () {
        // Now it is exposed
        window.test = this.test;
        },
        methods: {
        test: function() {
        alert("test fuction called");
        }
        }
        });

        // And later
        window.test();


        A better way of doing this is probably by using a global event bus. Instead of exposing random functions in the global scope, you can instead create a bus that you expose instead. The nice part about that is that if everything happens within the Vue application, you could use this.$bus.$emit('...') from anywhere in the Vue application and listen to it everywhere else in the Vue application. The nice part if it is used outside the Vue application is that you use a set interface between the inside of your Vue application and the outside of your Vue application, preventing you from having to expose more and more functions in the global scope, and allowing you to figure out what can and cannot be done from outside the Vue application.



        import Vue from 'vue';
        export const bus = new Vue();

        // Elsewhere
        import { bus } from './bus';
        Vue.prototype.$bus = bus;

        // In outside code
        import { bus } from '../../my-vue-application/bus';
        bus.$emit('test');

        // In your component
        var viewModel = new Vue({
        el: "#app",
        data: {},
        created () {
        this.$bus.$on('test', this.test);
        },
        beforeDestroy () {
        this.$bus.$off('test', this.test);
        },
        methods: {
        test: function() {
        alert("test fuction called");
        }
        }
        });





        share|improve this answer












        First of all, it seems like you are attaching a click handler the "Vue" way, without actually it being a Vue component. That is not going to work.



        To strictly achieve what you want, you have to expose your function to a different scope, e.g. via assigning it to a window attribute.



        var viewModel = new Vue({
        el: "#app",
        data: {},
        created () {
        // Now it is exposed
        window.test = this.test;
        },
        methods: {
        test: function() {
        alert("test fuction called");
        }
        }
        });

        // And later
        window.test();


        A better way of doing this is probably by using a global event bus. Instead of exposing random functions in the global scope, you can instead create a bus that you expose instead. The nice part about that is that if everything happens within the Vue application, you could use this.$bus.$emit('...') from anywhere in the Vue application and listen to it everywhere else in the Vue application. The nice part if it is used outside the Vue application is that you use a set interface between the inside of your Vue application and the outside of your Vue application, preventing you from having to expose more and more functions in the global scope, and allowing you to figure out what can and cannot be done from outside the Vue application.



        import Vue from 'vue';
        export const bus = new Vue();

        // Elsewhere
        import { bus } from './bus';
        Vue.prototype.$bus = bus;

        // In outside code
        import { bus } from '../../my-vue-application/bus';
        bus.$emit('test');

        // In your component
        var viewModel = new Vue({
        el: "#app",
        data: {},
        created () {
        this.$bus.$on('test', this.test);
        },
        beforeDestroy () {
        this.$bus.$off('test', this.test);
        },
        methods: {
        test: function() {
        alert("test fuction called");
        }
        }
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 8:05









        Sumurai8

        12.9k83160




        12.9k83160






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425795%2fcant-call-vue-component-method-outside-of-its-element%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...