我正在使用 vuex 和 vuejs。我目前有 2 个 vue 实例(我知道这很糟糕,但我不能这样做,这不是问题)
Sidebar.js vue 定义:
//VUEX stores
import { dashboardStore } from './../../js/Stores/DefinitionMappingStores/VuexStoreForDashboardPage'
//first vue with initialisation of state
var vmSidebar = new Vue({
el: '#sidebar-content',
store: dashboardStore,
created() {
this.$store.dispatch('search/setMiniWidgets', miniwidgetsfromjson);
},
})
仪表板.js:
import { dashboardStore } from './../../js/Stores/DefinitionMappingStores/VuexStoreForDashboardPage'
//second vue where state is empty (array(0) instead of having 20+ items inside)
var vm = new Vue({
el: '#dashboard-container',
store: dashboardStore,
mounted: function () {
let that = this; //take this reference, pointing to the current Vue instance
let grid = GridStack.init(options, this.$refs["dashboardref"].$el); //initialise gridstack grid (javascript lib)
grid.on('dropped', function (event, previousWidget, newWidget) { //here is a javascript event)
console.log(this); console.log(that); //here this is the dropped div element / that is the vue instance saved before... but it keep the $store variable not following change...
let vueMiniWidgetComponentFromSidebar = that.$store.getters['search/getFilteredMiniWidgetsById'](idFromNewAddedNode); //here the store search is not initialized but in the other vue from sidebar I see it initialized
});
},
})
搜索模块商店:
const searchModule = {
namespaced: true,
state: {
miniWidgets: []
},
getters: {
getFilteredMiniWidgetsById: (state) => (id) => {
if (id == undefined || id == null || id == "") {
return null;
} else {
return state.miniWidgets.find(miniWidget => miniWidget.Id === id)
}
}
},
}
我认为我的问题是我保存了 Vue 实例的“状态”,而不是对它的引用,所以每次调用我的 grid.on 函数时,指向保存的 Vue 实例的“那个”引用将是相同的(保存的)。
所以我的问题是:如何让我的 grid.on 函数获得正确的 vue 实例并改变相关的商店?以及如何在这两个文件中让相同的 Vuex 存储共享信息?
忽然笑
相关分类