Mr.*_*way 5 javascript arrays vue.js vue-component vuex
我仍在学习 vue.js 并且处于一个小项目的中间,以帮助我了解有关使用 Vuex 创建更大规模应用程序的更多信息。
我遇到了一个问题,我试图使用应用程序中的按钮从数组中删除特定项目;我似乎无法在 Vuex 中获得正确的语法来执行此操作。这是我正在使用的内容:
店铺
const state = {
sets: [{}]
}
export const addSet = function ({ dispatch, state }) {
dispatch('ADD_SET')
}
const mutations = {
ADD_SET (state) {
state.sets.push({})
},
REMOVE_SET (state, set) {
state.sets.$remove(set)
}
}
Run Code Online (Sandbox Code Playgroud)
行动
export const removeSet = function({ dispatch }, set) {
dispatch('REMOVE_SET')
}
Run Code Online (Sandbox Code Playgroud)
吸气剂
export function getSet (state) {
return state.sets
}
Run Code Online (Sandbox Code Playgroud)
应用程序
<div v-for="set in sets">
<span @click="removeSet">x</span>
<Single></Single>
</div>
Run Code Online (Sandbox Code Playgroud)
我有一个操作设置,它将向数组添加一个空白项目,当您单击add item
按钮时,它将在应用程序中放置一个新组件,只是不确定如何remove item
向每个组件添加一个按钮并使其工作。
我希望这是有道理的!
您需要修改removeSet
函数以分派set
要从存储中删除的对象:
export const removeSet = function({ dispatch }, set) {
dispatch('REMOVE_SET', set)
}
Run Code Online (Sandbox Code Playgroud)
然后在您的组件中,您可以通过事件调用操作@click
,但您需要传递set
对象:
<div v-for="set in sets">
<span @click="removeSet(set)">x</span>
<Single></Single>
</div>
Run Code Online (Sandbox Code Playgroud)
最后,不要忘记在组件中注册您的getters
和:actions
vuex: {
getters: {
getSet
},
actions: {
addSet, removeSet
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7576 次 |
最近记录: |