我在 vue.js 中有一些方法,我基本上为不同的组件回收相同的方法,这意味着在我的方法中,只有一些小参数会针对不同的组件进行更改。我想将这些组件传递给我的函数,然后该函数应该返回另一个函数,该函数具有我之前定义的参数变量各自的范围,例如:
retrieveSomeRoomData:Helper.retrieveData(pagetype = 'someCountry', roomType = 'someRoom')
现在,这个retrieveData函数是在我的Helper中定义的:
检索数据:((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
})();
现在我认为上述内容可以通过函数柯里化(Currying)实现,我在真实函数定义内的外部函数作用域上传递自定义参数并返回内部自定义函数...但我收到错误 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')
最佳答案
这可能是一个简单的大小写错误,如果没有完整的代码很难知道。函数定义中有驼峰式大小写 (pageType),但错误 (pagetype) 中没有驼峰式大小写。
关于javascript 函数柯里化(Currying)将默认参数传递给 vue.js 中的外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454113/