es6 curry function
es6 curry function
// vuex getters
export const getAdsFilterConfig = (state) => (spreader) => {
console.log('state =', state)
console.log('spreader =', spreader)
return state[spreader];
};
// this.getAdsFilterConfig(this.spreaderAlias);
// export const stateActivity = state => state.stateActivity;
equal to
// getter 自动执行,getter func === func()
export const getAdsFilterConfig = (state) => {
// state 来自 store 上下文
return (spreader) => {
return state[spreader];
};
}
https://codesandbox.io/s/vuex-store-getter-es6-curry-function-n7prr?file=/src/main.js
bug
state = {k: 1};
// {k: 1}
getAdsFilterConfig = (state) => (spreader) => {
return state[spreader];
};
/*
(state) => (spreader) => {
return state[spreader];
}
*/
getAdsFilterConfig(`k`);
/*
(spreader) => {
return state[spreader];
}
*/
getAdsFilterConfig()(`k`);
// Uncaught TypeError: Cannot read property 'k' of undefined
vuex
const store = new Vuex.Store({
state: {
todos: [
{ id: 1, text: '...', done: true },
{ id: 2, text: '...', done: false }
]
},
getters: {
doneTodos: state => {
return state.todos.filter(todo => todo.done)
}
}
})
https://vuex.vuejs.org/guide/getters.html#method-style-access
refs
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14208294.html
未经授权禁止转载,违者必究!