karol-f / vue-custom-element

Vue Custom Element - Web Components' Custom Elements for Vue.js
https://karol-f.github.io/vue-custom-element/
MIT License
1.97k stars 187 forks source link

Is it possible to not expose methods via getVueInstance(). #132

Closed toams69 closed 6 years ago

toams69 commented 6 years ago

I don't think the user of the webcomponent need to know that the component has vue embeded. This is a good thing to have but I would prefer to have directly access to the method. Is it possible to have access directly to method via the element ?

oskarrough commented 6 years ago

I also wanted this for a component. Here's one approach you can try:

Vue.customElement('my-element', VueComponent, {
    connectedCallback() {
        const setPublicMethods = (event) => {
            const instance = this.getVueInstance()
            this.myMethod = instance.myMethod
        }

        this.addEventListener('vce-ready', setPublicMethods)
    },

    disconnectedCallback() {
        this.removeEventListener('vce-ready', setPublicMethods)
    }
})

// document.querySelector('my-element').myMethod()
karol-f commented 6 years ago

Hi, getVueInstance() method is indeed accessible to users. You can use, a little bit changed, above code to remove getVueInstance():


const removeVueInstanceGetter = (event) => {
    this.getVueInstance = () => {};
}

Vue.customElement('my-element', VueComponent, {
    connectedCallback() {
        this.addEventListener('vce-ready', removeVueInstanceGetter)
    },

    disconnectedCallback() {
        this.removeEventListener('vce-ready', removeVueInstanceGetter)
    }
})

I don't think that adding another option just to remove it is necessary. What do you think?