简体   繁体   中英

How can I integrate Vuex into a Storyblok plugin?

I'm building a plugin for storyblok with vue. I decided it would be easier to add state management to the plugin rather than an echo chamber of $emit() all the way back up the component tree. I installed Vuex into my plugin and went to add it to the main.js file as instructed in a Vuex tutorial however my main.js file isn't set up like a regular Vue app's main.js file.

The tutorial tells us to do this

import Vue from 'vue'
import App from './App.vue'
import store from './store' //our Vuex store

Vue.config.productionTip = false

new Vue({
    store, //passing in our store
    render: h =>(App)
}).$mount('#app')

However my main.js file due to being a storyblok plugin looks like this

import Plugin from './Plugin.vue'
import store from './store'  //I have no clue where to put this in the code below :(

if (process.env.NODE_ENV == 'development') {

  window.Fieldtype = Plugin
  let customComp = window.Storyblok.vue.extend(window.Fieldtype);
  window.Storyblok.vue.component('custom-plugin', customComp);
  window.StoryblokPluginRegistered = true;

} else {
  
  let init = Plugin.methods.initWith() 
  window.storyblok.field_types[init.plugin] = Plugin

}

As you can see the setup is totally different as it's geared towards injecting a Vue component into storyblok as a plugin rather than setting up a new Vue app. Does anybody know what I should do here?

I solve that issue adding in the main.js this line: window.Storyblok.vue.$store = store;

Then in your plugin when you call the store use the same. For example: window.Storyblok.vue.$store.commit()

So I am still learning StoryBlok and their plugin development, so please yell at me if this is wrong.

I was able to solve this like so.

Just for some context this is my general folder structure.

.
└── my-app/
    ├── node_module/
    │   └── ...
    ├── public/
    │   └── index.html
    └── src/
        ├── store/
        │   ├── state.js
        │   └── config.js
        ├── main.js
        └── Plugin.vue

main.js

import Vuex from 'vuex'
window.Storyblok.vue.use(Vuex); // This has to come before the Plugin import
import Plugin from './Plugin.vue'

if (process.env.NODE_ENV == 'development') {

  window.Fieldtype = Plugin

  let customComp = window.Storyblok.vue.extend(window.Fieldtype);
  window.Storyblok.vue.extend(window.Fieldtype);
  window.Storyblok.vue.component('custom-plugin', customComp);
  window.StoryblokPluginRegistered = true;

} else {

  let init = Plugin.methods.initWith()
  window.storyblok.field_types[init.plugin] = Plugin

}

Plugin.vue

import Vuex from 'vuex';
import State from './store/state';

const store = new Vuex.Store(State);

export default {
  name: 'my-vue-plugin',

  mixins: [window.Storyblok.plugin],

  store: store,

  components: {},

  data() {
    return {}
  },
};

state.js

import Config from './config';

export default {
    modules: {
        Config
    },
    state: {
        count: 0
    },
    mutations: {
        increment(state) {
            state.count++
        }
    }
};

Then anywhere in your nested components or whatever you can call the increment mutation like so this.$store.commit('increment');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM