Я хочу использовать данные моего магазина Vuex в своем приложении и шаблоне.
Мой магазин Veux:
var Vue = require('vue');
var Vuex = require('vuex');
Vue.use(Vuex)
let store = new Vuex.Store({
state: {
user: {},
},
}
}
module.exports = store
Я хочу использовать данные магазина в своем приложении и отображать их в шаблоне. Но при установке данных в свойстве data
this.$store
равно undefined
. Однако я могу получить к нему доступ с помощью функции beforeRender
:
var Vue = require('vue'),
store = require('../../link/to/my/store');
module.exports = {
el: '#selector',
store,
components: {},
data: {
saving: false,
user: this.$store.state.user // this.state is not available here
},
created: function(){},
beforeCreate: function() {
console.log(this.$state.state) // this.$state is available here
// i get my data in the form of a json string from the dom
// and update the global store with the data
let user = document.querySelector('#user-data')
if (user) this.$store.dispatch('updateUser', JSON.parse(user.innerHTML))
},
methods: {}
}
};
Я также пробовал использовать вычисленное свойство, но безрезультатно.