Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish an Vue.js NPM package with a Vuex module?

I am developing some Vue components that I would like to place in an NPM package. The problem that I am facing is that I have no idea how to publish components that rely on a Vuex module.

I have neatly placed all the code needed for this library of components into a separate Vuex module but how do I register my module when somebody imports my package?

A good start would be creating a plugin I guess but I would still need to check for a Vuex instance and somehow register my module.

I've seen plenty of tutorials on how to publish Vue components but not something more complex.

Any suggestions?

like image 222
Stephan-v Avatar asked Jul 24 '17 10:07

Stephan-v


2 Answers

This is also a helpful reference for anyone trying to figure this out:

https://forum.vuejs.org/t/how-to-create-an-npm-package-of-a-vue-js-project-which-uses-vuex/14706/2

A key thing to notice here is the usage of:

store.registerModule('desiredModuleName', yourModule)

It is a bit hidden away in the API, but this allows you to register your Vuex module as long as users pass in their store.

https://vuex.vuejs.org/en/modules.html#dynamic-module-registration

like image 75
Stephan-v Avatar answered Oct 13 '22 22:10

Stephan-v


You should be able to just ask the user to add your vuex module (expose it as part of your packages public api) to your modules. Maybe that could be done as part of the installation function as well (if you use the plugin approach).

like image 21
blockhead Avatar answered Oct 13 '22 21:10

blockhead