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

What about Mixins and methods #229

Closed victorantos closed 3 years ago

victorantos commented 3 years ago

getProps and exctractProps is in place to be used with Mixins,

shouldn't we have something similar for component methods ? https://github.com/karol-f/vue-custom-element/blob/ed702bcd6daee65078b81f0b3055d0f4b151bf3d/src/utils/props.js#L54

karol-f commented 3 years ago

This linked code is for checking if mixin has some props and we need to expose them in vue-custom-element.

Can You please write more how it's connected to methods and what You want to achieve?

If just accessing methods from underlying Vue component, You have getVueInstance() e.g.

document.querySelector('widget-vue').getVueInstance()

Regards.

victorantos commented 3 years ago

I need to check if mixin has some methods and I need to expose them in.

My use case is to add a mixin method that getsDataFromServer() and this is exposed to any component for my custom element. Basically I want to share a method with all my components and Mixin should do that. But it does not work with vue-custom-element, throws some error on the getProps line

karol-f commented 3 years ago

Ok, so using getVueInstance() will expose whole Vue instance and You have there methods from mixins.

victorantos commented 3 years ago
export const myDataMixin = {
    props: {
        title: String,
        default: "empty"
    },
    methods: {
        getData: function ()
        {         
         }
    }
}

getData() method is not working when I apply this mixin

karol-f commented 3 years ago

Is it working when applied to Vue component and accesed e.g. as this.getData()?

If so getVueInstance() will get You root Vue instance of Your custom element.

victorantos commented 3 years ago

ok thank you.

I think this is working for me, I can call getData from App component

`Vue.mixin(
  {
    props: {
      title: String,
      default: "empty"
    },
    methods: {
      getData: function () {
        console.log('Done');
      }
    }
  });

Vue.customElement('my-widget', App);`
victorantos commented 3 years ago

So my solution was to make it a Global Mixin instead of Local Mixin.

This will not work with vue custom element

<script> import clickMixin from '../Mixins/clickMixin' export default { name: 'Test', mixins: [clickMixin] } </script>

karol-f commented 3 years ago

Maybe register mixing before creating custom element and it should be available in it