admin管理员组文章数量:1398763
In my App.vue I am dispatching a Vuex action to retrieve a user
from my API and also the role
of the user like this:
async mounted() {
const userToken = localStorage.getItem('token');
await this.getUserRole(userToken);
await this.getUserImage(userToken);
},
I wrote a getter that only returns me the current role that is saved in the user object like this getRole: state => state.user_role.role
What I want to do is, that I want to dispatch an action regarding the user role in another view (Projects.vue):
async mounted() {
console.log(this.$store.getters['user/getRole']);
console.log(this.userRole);
if (this.userRole === 'user') {
// do things ...
}
}
puted: {
...mapGetters('user', {
user: 'getUser',
userRole: 'getRole',
})
}
The problem here is that at the first page load/refresh I get undefined
back from my getter. Only if I switch the route and e back, it returnes me in this case user
instead of undefined
.
I mean I could dispatch the user/getUserRole
action again in my Project.vue but I guess that would be bad as I have multiple calls for the same store/action.
I also tried to work with $nextTick()
in my mounted()
but this did also not help.
In my App.vue I am dispatching a Vuex action to retrieve a user
from my API and also the role
of the user like this:
async mounted() {
const userToken = localStorage.getItem('token');
await this.getUserRole(userToken);
await this.getUserImage(userToken);
},
I wrote a getter that only returns me the current role that is saved in the user object like this getRole: state => state.user_role.role
What I want to do is, that I want to dispatch an action regarding the user role in another view (Projects.vue):
async mounted() {
console.log(this.$store.getters['user/getRole']);
console.log(this.userRole);
if (this.userRole === 'user') {
// do things ...
}
}
puted: {
...mapGetters('user', {
user: 'getUser',
userRole: 'getRole',
})
}
The problem here is that at the first page load/refresh I get undefined
back from my getter. Only if I switch the route and e back, it returnes me in this case user
instead of undefined
.
I mean I could dispatch the user/getUserRole
action again in my Project.vue but I guess that would be bad as I have multiple calls for the same store/action.
I also tried to work with $nextTick()
in my mounted()
but this did also not help.
2 Answers
Reset to default 5Watch the puted, and then execute the code once the user role is set, for example:
puted: {
...mapGetters('user', {
user: 'getUser',
userRole: 'getRole',
})
},
watch: {
userRole: {
handler(role) {
if (role) {
this.yourAction();
}
},
immediate: true
}
},
methods: {
yourAction() {
console.log('got role:', this.userRole);
}
}
The immediate
property is used within the watcher for userRole
so that the handler will be called immediately when the ponent is loaded.
Since both routes need the same data, and the user can pretty much decide what route to access first -- you might want to initiate the API call in both routes, only adding a flag to it whether or not the specific call has already been initiated by other routes.
In your store states, you can define some sort of collection that holds boolean properties.
api_calls_ok: {
user_data: false
}
Then you can define an action that does similar to this
const userToken = localStorage.getItem('token'); //side note: you can use localStorage.token instead
await this.getUserRole(userToken);
await this.getUserImage(userToken);
Maybe something like
getUserData({ mit, state }){
if(!state.api_calls_ok.user_data){
dispatch('getUserRole');
dispatch('getUserImage');
mit('flagApiCalls', 'user_data', true); //set the boolean to true
}
}
In App.vue and Project.vue, you can just dispatch the getUserData
during mount and not worry about calling the API twice or more. After dispatching, just access your getter.
本文标签: javascriptVuex Return getter in mounted hookStack Overflow
版权声明:本文标题:javascript - Vuex: Return getter in mounted hook - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744702256a2620627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论