将 vue js 组件参数传递给 vuex 方法样式 getter 参数
pass vue js component parameter to vuex method-style getter parameter
vuex 新手。应该按世代和类型显示口袋妖怪的简单口袋妖怪 SPA。我正在尝试编写一个方法样式 getter 来传递组件的参数。编辑:将 currentType 移动到状态:
//Vuex.Store
state: {
pokemon: [],
currentType: 'all'
},
getters:{
availablePokemon: (state) => (currentType) => {
if(!currentType || currentType === 'all'){
return state.pokemon
}else{
return state.pokemon.filter(pokemon => {
//some api objects have two types
if(pokemon.types.length === 2){
if(pokemon.types[0] == currentType || pokemon.types[1] == currentType){
return true
}
}else if(pokemon.types[0] == currentType){
return true
}
})
}
},
mutations:{
changeType(state, type){
state.currentType = type
}
},
actions:{
updateType(context, type){
context.commit('changeType', type)
}}
}
编辑:
//Pokemon.vue
<select name="type" id="type" @change="updateType($event.target.value)">
<option v-for="(type, index) in types"
:key="index"
:value="type">{{ type }}</option>
</select>
<li v-for="(pokemon, index) in allPokemon"
:key="index">
{{ pokemon.name }}
</li>
export default {
data(){
return{
types: ['all', 'bug', 'dragon'],
}
},
computed: {
allPokemon(){
//this fails, says currentType is undefined
return this.$store.getters.availablePokemon(currentType)
}
},
methods: {
updateType(type){
this.$store.dispatch('updateType', type)
}
}
组件方法样式 getter 无法识别参数。我已经尝试将 currentType 移动到我的商店(并使用 action => mutation => 等更新它),但这也没有用。有什么想法我想念的吗?
在架构上,我会将 currentType
存储在
Vuex 状态对象。然后,您可以在 getter 函数中引用 currentType
- 并且 - 还可以在应用程序范围内引用 currentType
。
JSFiddle 供您参考(参见 javascript 选项卡和控制台输出):https://jsfiddle.net/qb47x2z1/38/
vuex 新手。应该按世代和类型显示口袋妖怪的简单口袋妖怪 SPA。我正在尝试编写一个方法样式 getter 来传递组件的参数。编辑:将 currentType 移动到状态:
//Vuex.Store
state: {
pokemon: [],
currentType: 'all'
},
getters:{
availablePokemon: (state) => (currentType) => {
if(!currentType || currentType === 'all'){
return state.pokemon
}else{
return state.pokemon.filter(pokemon => {
//some api objects have two types
if(pokemon.types.length === 2){
if(pokemon.types[0] == currentType || pokemon.types[1] == currentType){
return true
}
}else if(pokemon.types[0] == currentType){
return true
}
})
}
},
mutations:{
changeType(state, type){
state.currentType = type
}
},
actions:{
updateType(context, type){
context.commit('changeType', type)
}}
}
编辑:
//Pokemon.vue
<select name="type" id="type" @change="updateType($event.target.value)">
<option v-for="(type, index) in types"
:key="index"
:value="type">{{ type }}</option>
</select>
<li v-for="(pokemon, index) in allPokemon"
:key="index">
{{ pokemon.name }}
</li>
export default {
data(){
return{
types: ['all', 'bug', 'dragon'],
}
},
computed: {
allPokemon(){
//this fails, says currentType is undefined
return this.$store.getters.availablePokemon(currentType)
}
},
methods: {
updateType(type){
this.$store.dispatch('updateType', type)
}
}
组件方法样式 getter 无法识别参数。我已经尝试将 currentType 移动到我的商店(并使用 action => mutation => 等更新它),但这也没有用。有什么想法我想念的吗?
在架构上,我会将 currentType
存储在
Vuex 状态对象。然后,您可以在 getter 函数中引用 currentType
- 并且 - 还可以在应用程序范围内引用 currentType
。
JSFiddle 供您参考(参见 javascript 选项卡和控制台输出):https://jsfiddle.net/qb47x2z1/38/