[Vuejs]-Vue2 – What does a public/private method implementation look like in mixins?

1👍

In Vue 2, there is no built-in concept of "private" methods, but you can mimic this behavior by defining functions outside the component or mixin object, just like in the example you’ve provided. These functions will not be accessible from other components or mixins, effectively making them "private".

The "even better" approach you’ve mentioned involves defining a private function outside the mixin object. This function is only accessible within the same file/module and is not exposed when the mixin is imported into a component. Here’s an example of how you might use this approach:

So, define mixin like this:

function privateMethod() {
  console.log('Private method firing');
}

export default {
  methods: {
    publicMethod() {
      console.log('Public method firing');
      privateMethod();
    }
  }
}

and on the component:

import myMixin from './myMixin';

export default {
  mixins: [myMixin],
  mounted() {
    this.publicMethod(); // This will call both the public and private methods
  }
}

So, you can some how mimic the behavior like that.

Hope this help!

Leave a comment