Last active
October 19, 2019 15:29
-
-
Save RuslanHolovko/0a5c2a61eaa0ff0984643b3aea4186da to your computer and use it in GitHub Desktop.
get element by Id Vue (getters)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// store.js | |
export default new Vuex.Store({ | |
state:{ | |
tasks: JSON.parse(localStorage.getItem('tasks') || '[]') | |
}, | |
mutations: { | |
createTask(state, payload){ | |
state.tasks.push(payload); | |
localStorage.setItem('tasks', JSON.stringify(state.tasks)); | |
}, | |
actions:{ | |
createTask({commit}, payload){ | |
commit('createTask', payload | |
} | |
}, | |
getters: { | |
tasks: s => s.tasks, | |
taskById: s => id => s.tasks.find(t => t.id === id), | |
operatorActive: (state) => { | |
return (name) => { | |
for (const key in state.operators) { | |
if (state.operators.hasOwnProperty(key)) { | |
const element = state.operators[key]; | |
if(element.routerName === name){ | |
return element | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}); | |
// component.vue | |
export default{ | |
computed:{ | |
task(){ | |
return this.$store.getters.taskById(parseInt(this.$route.params.id)); | |
// OR | |
return this.$store.getters.taskById(+this.$route.params.id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment