matt
matt

Reputation: 2039

Laravel: Vue is not defined for plugin

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?

Upvotes: 0

Views: 100

Answers (1)

Mad an
Mad an

Reputation: 159

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');

Upvotes: 1

Related Questions