我正在用vue为故事博客构建一个插件。我决定将状态管理添加到插件中会更容易,而不是在组件树上一路回显$emit()的回显腔。我将Vuex安装到插件中,并按照Vuex教程中的指示将其添加到main.js文件中,但是main.js文件的设置并不像普通Vue应用程序的main.js文件。
本教程告诉我们要这样做。
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')但是,由于我的main.js文件是一个storyblok插件,所以看起来如下所示
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
}正如您所看到的,设置是完全不同的,因为它的目的是将Vue组件作为插件注入storyblok,而不是设置一个新的Vue应用程序。有人知道我该怎么做吗?
发布于 2022-04-05 10:44:24
我解决了这个问题,在main.js中添加了以下一行:window.Storyblok.vue.$store = store;
然后在你的插件,当你打电话给商店,使用同样的。例如:window.Storyblok.vue.$store.commit()
发布于 2022-08-09 12:14:05
所以我仍然在学习StoryBlok和他们的插件开发,所以如果这是错误的,请对我大喊大叫。
我就这样解决了这个问题。
只是为了一些上下文,这是我的一般文件夹结构。
.
└── my-app/
├── node_module/
│ └── ...
├── public/
│ └── index.html
└── src/
├── store/
│ ├── state.js
│ └── config.js
├── main.js
└── Plugin.vuemain.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++
}
}
};然后在嵌套组件中的任何地方,或者任何您可以称之为increment变异的地方,就像this.$store.commit('increment');一样
https://stackoverflow.com/questions/69126947
复制相似问题