Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Vue 3 (errors due to Global API changes) #11

Open
akserikawa opened this issue Jan 23, 2023 · 2 comments
Open

Add support for Vue 3 (errors due to Global API changes) #11

akserikawa opened this issue Jan 23, 2023 · 2 comments

Comments

@akserikawa
Copy link

akserikawa commented Jan 23, 2023

Trying to install the plugin on a Vue 3 project (w/ Nuxt 3) it errors:

500
Cannot set properties of undefined (setting 'pusher')

at Object.install (http://localhost:3000/_nuxt/node_modules/.vite/deps/vue-pusher.js?v=ae3370f2:6061:30)
at Object.use (http://localhost:3000/_nuxt/node_modules/.vite/deps/vue.js?v=ae3370f2:4690:18)
at http://localhost:3000/_nuxt/plugins/pusher.client.js:4:18
at fn (http://localhost:3000/_nuxt/node_modules/.pnpm/[email protected]/node_modules/nuxt/dist/app/nuxt.mjs?v=ae3370f2:141:27)
at callWithNuxt (http://localhost:3000/_nuxt/node_modules/.pnpm/[email protected]/node_modules/nuxt/dist/app/nuxt.mjs?v=ae3370f2:146:12)
at applyPlugin (http://localhost:3000/_nuxt/node_modules/.pnpm/[email protected]/node_modules/nuxt/dist/app/nuxt.mjs?v=ae3370f2:92:29)
at applyPlugins (http://localhost:3000/_nuxt/node_modules/.pnpm/[email protected]/node_modules/nuxt/dist/app/nuxt.mjs?v=ae3370f2:101:11)
at async initApp (http://localhost:3000/_nuxt/home/akiraserikawa/Sites/nuxt-demo/node_modules/.pnpm/[email protected]/node_modules/nuxt/dist/app/entry.mjs:39:7)

this caused by changes in Vue 3, where Vue.prototype does not exist anymore, so the plugin cannot work:

module.exports = {
    install: function (Vue, options) {
        var pusher = new VuePusher(options.api_key, options.options);

        Vue.prototype.pusher  = pusher;
        Vue.prototype.$pusher = pusher.pusher; // Just in case they want to manage it themselves.
    }
};

I guess it would require the plugin to use app.config.globalProperties instead.

https://vuejs.org/api/application.html#app-config-globalproperties

@akserikawa
Copy link
Author

Managed to make it work with this workaround:

// plugins/pusher.js

import Pusher from "pusher-js";

export default defineNuxtPlugin((nuxtApp) => {
  const Vue3Pusher = {
    install: (app, options) => {
      var pusher = new Pusher(options.api_key, options.options);

      app.config.globalProperties.$pusher = pusher;
    },
  };

  nuxtApp.vueApp.use(Vue3Pusher, {
    api_key: "****",
    options: {
      cluster: "eu",
    },
  });
});

@Philword
Copy link

Managed to make it work with this workaround:

// plugins/pusher.js

import Pusher from "pusher-js";

export default defineNuxtPlugin((nuxtApp) => {
  const Vue3Pusher = {
    install: (app, options) => {
      var pusher = new Pusher(options.api_key, options.options);

      app.config.globalProperties.$pusher = pusher;
    },
  };

  nuxtApp.vueApp.use(Vue3Pusher, {
    api_key: "****",
    options: {
      cluster: "eu",
    },
  });
});

How did you use it inside component?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants