2

I'm trying to create a global helper function via Vue mixin on a Laravel Inertia project to no avail:

//app.js
Vue.mixin({
    methods: {
        myFunction() {
            return 'Returnign from myFunction';
        },
    },
});

new Vue({
...
}).$mount(app);

.

//MyComponent.vue
console.log(myFunction()); // ReferenceError: myFunction is not defined

On a standalone Vue.JS project, this works. Maybe there's something behind the scene in Inertia that prevents the mixin from loading. Can somebody help me understand why this is happening?

Thank you.

2 Answers 2

0

You need to add the function in the mixin of the createInertiaApp method in your app.js file.

For instance:

createInertiaApp({
  resolve: (name) => require(`./Pages/${name}.vue`),
  setup ({ el, app, props, plugin }) {
    return createApp({ render: () => h(app, props) })
      .use(plugin)
      .mixin({
        methods: {
          myFunction: () => {
            return 'Returning from myFunction';
          }
        }
      })
      .mount(el)
  }
})
Sign up to request clarification or add additional context in comments.

Comments

0

you need () on your function

Vue.mixin({
    methods: {
        myFunction() {
            return 'Returnign from myFunction';
        },
    },
});

and then you missed this before your mixin function

console.log(this.myFunction());
    

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.