1

I have a simple Vue demo that shows a user a modal when they click a button:

http://plnkr.co/edit/lZPD7HOjKFsNVNmnWAOB?p=preview

The problem is, I'm also trying to use a plugin that generates a QR code that would go inside of the modal, so I set up a method inside my component that uses $dispatch to call a function called showQR:

    methods: {
                showQR: function(){
                     console.log("Dispatching event for QR code")
                     this.$dispatch('showQR');
                     return this.showModal = true    
            }
     }

The function inside my parent Vue object looks like this:

new Vue({
        el: 'body',
        events: {
            showQR: function(){
                console.log("Inside the parent Vue")
                new QRCode(document.getElementById("qrcode"), "http://stackoverflow.com/");
            }
        }
    });

I do get the message "Dispatching event for QR code" from the component, but function on the parent Vue isn't working. I can get the QR code to show up if I put the logic inside of the template, but I'd rather avoid that. Any suggestions on how to make $dispatch work? Am I targeting the right Vue? Using VueRouter for this btw. Any help would be great!

1 Answer 1

3

Looks like your main App logic gets overriden by this:

var App = Vue.extend({
    template: '<router-view></router-view>'
})

If you combine your main methods object with this extend, you should be fine. So, something like this would work:

var App = Vue.extend({
    el: 'body',
    events: {
        showQR: function(){
            console.log("Inside the parent Vue")
            new QRCode(document.getElementById("qrcode"), "http://stackoverflow.com/");
        }
    },
    template: '<router-view></router-view>'
})

You also most likely don't want/need el: 'body', in there.

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

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.