Home > Mobile >  Laravel: Vue is not defined for plugin
Laravel: Vue is not defined for plugin

Time:06-17

I am trying to create a plugin for global functions in Laravel 8.4 / VueJs 2. I have tried creating a file plugin.js in the same folder as app.js

plugin.js

const Plugin = {

  install(Vue, options) {

    Vue.prototype.toTitleCase = (str) => {
        return str.split(' ')
            .map(w => w[0].toUpperCase()   w.substring(1).toLowerCase())
            .join(' ');
    }

  },
}

Vue.use(Plugin)

app.js

import Vue from 'vue';
require('./plugin.js');

However I keep getting the error:

Uncaught ReferenceError: Vue is not defined
    at Object../resources/js/plugin.js

I have tried doing the import int the plugin file. However this just leads to more errors. What am I doing wrong?

CodePudding user response:

Initialize vue on window using window.Vue = require('vue').default; Your app.js should look like

window.Vue = require('vue').default;
import Vue from 'vue';
require('./plugin.js');
  • Related