【发布时间】:2019-12-23 10:55:41
【问题描述】:
我在 vue.js 中有一些方法,我基本上为不同的组件回收了相同的方法,这意味着在我的方法中,不同的组件只改变了一些小的参数。我想将这些组件传递给我的函数,然后该函数应该返回另一个函数,该函数具有我先前定义的参数变量各自的范围,例如:
retrieveSomeRoomData: Helper.retrieveData(pagetype = 'someCountry', roomType = 'someRoom')
现在,我的 Helper 中定义了这个 retrieveData 函数:
retrieveData: ((pageType, roomType) => {
return ({ commit, state }, payload) => {
...doSomething with payload and state, and use the pageType and roomType variables for
customizing the function... then return this function
})();
现在我认为通过函数柯里化可以实现上述操作,我将自定义参数传递到我的真实函数定义内的外部函数范围并返回该内部自定义函数......但我得到 pageType 和 roomType 不是的错误已定义...我什至不确定,如果我使用上面第一行代码中的函数,我的内部函数的提交、状态和有效负载参数是否正在获取它们的参数值...我只传递了一些默认参数,但我不确定是否传递了内部函数的其他参数......我怎样才能做到这一点最好?
编辑:我收到以下错误:
Uncaught ReferenceError: pagetype is not defined
at eval (actions.js?63e0:13)
at Module../src/store/actions.js (<anonymous>:9209:1)
at __webpack_require__ (<anonymous>:727:30)
at fn (<anonymous>:101:20)
at eval (store.js?07a4:1)
at Module../src/store/store.js (<anonymous>:9245:1)
at __webpack_require__ (<anonymous>:727:30)
at fn (<anonymous>:101:20)
at eval (VM6658 main.js:12)
at Module../src/main.js (<anonymous>:9197:1)
嗯,是不是因为我必须做 const pagetype= 'someCountry' 和 const roomType?
编辑:我找到了解决方案... 在调用我的函数时,我只需要使用纯字符串作为参数:
Helper.retrieveData('someCountry','someRoom')
【问题讨论】:
-
这应该可以——您介意提供更多细节来帮助调试吗?
-
我在上面编辑了它,也许我只是忘记了 let 或 const 来定义默认参数?编辑:不,这显然是不可能的;)
-
repl.it/repls/PreciousEqualDigits,我认为调用函数时不需要分配参数
标签: javascript vue.js scope parameter-passing currying