link2740 link2741 link2742 link2743 link2744 link2745 link2746 link2747 link2748 link2749 link2750 link2751 link2752 link2753 link2754 link2755 link2756 link2757 link2758 link2759 link2760 link2761 link2762 link2763 link2764 link2765 link2766 link2767 link2768 link2769 link2770 link2771 link2772 link2773 link2774 link2775 link2776 link2777 link2778 link2779 link2780 link2781 link2782 link2783 link2784 link2785 link2786 link2787 link2788 link2789 link2790 link2791 link2792 link2793 link2794 link2795 link2796 link2797 link2798 link2799 link2800 link2801 link2802 link2803 link2804 link2805 link2806 link2807 link2808 link2809 link2810 link2811 link2812 link2813 link2814 link2815 link2816 link2817 link2818 link2819 link2820 link2821 link2822 link2823 link2824 link2825 link2826 link2827 link2828 link2829 link2830 link2831 link2832 link2833 link2834 link2835 link2836 link2837 link2838 link2839 link2840 link2841 link2842 link2843 link2844 link2845 link2846 link2847 link2848 link2849 link2850 link2851 link2852 link2853 link2854 link2855 link2856 link2857 link2858 link2859 link2860 link2861 link2862 link2863 link2864 link2865 link2866 link2867 link2868 link2869 link2870 link2871 link2872 link2873 link2874 link2875 link2876

[Vue.js] Better approach handling 'Do not mutate vuex store state outside mutation handlers' errors

Beforehand: My application is working as intended, but to know if there’s an better approach to the problem, I was having.

Situation: there is a project where when currently implemeneting a Permission-System. The current flow is, to load specific objects (lets take user in this case) and inject the permissions afterwards.

Problem: Getting ‘Do not mutate vuex store state outside mutation handlers.’ error inside vuex-action.

Question: Is there a better way to omit the error than my approach below?

Simplified it looks like this (here when getting my objects from our API and storing them in vuex-store):

// user.js (vuex-module)
state: {
user: null,
mutations: {
// …
setUser(state, user) {
state.user = user
actions: {
// … other vuex-actions
async login({commit, dispatch}, payload) {
let userFromDb = DbUtil.getUser(payload) // is an axios call to our api

// here the permissions get injected
// action in another vuex-module
dispatch(‘permissions/injectPermissions’, userFromDb)

// commiting to store
commit(‘setUser’, userFromDb)

return userFromDb

My permissions.js (here when injecting the permissions to my object):

// permissions.js (vuex-module)
actions: {
// … other vuex-actions

// payload = user in this example
async injectPermissions({commit, dispatch}, payload) {
let permissionFromDb = DbUtil.getPermissions(/* … */)

payload.permissions = permissionFromDb // -> Here when getting ‘Do not mutate vuex store state outside mutation handlers.’-Error, because `payload` == the user from user-state

return payload

Workaround: I added a mutation which changes the user-state object for me inside a mutation-handler.

mutations: {
* A ‘set’-wrapper to mutate vuex-store variable inside a mutation to avoid getting a error.
* @param state
* @param payload:
* object - object to be mutated
* prop - prop inside object that is affected
* value - value that should be assigned
setWrapper(state, payload) {
let { object, prop, value } = payload

object[prop] = value

The line where the error was thrown before gets changed to:

commit(‘setWrapper’, {
object: payload,
prop: ‘permissions’,
value: permissionFromDb

Solution :

Actions do not mutate the state.
Actions are there to perform asynchronous tasks.
When you want to change the state within an action, you have to rely on a mutation by using this synatx: commit(‘MUTATION_NAME’, payload)


MUATATION_NAME(state, payload) {
state.permissions = payload.permissions

This is the clean and right way.