[英]JavaScript - Pass second argument to ES6 Template whilst using .map
我正在使用es6模板,但是在尝试将第二个参数传递给原始内部使用的另一个模板时遇到问题。
最初,我只使用result.relevantJobTypes
参数,但是现在我试图也将result.id
传递给模板使用,但是由于map
原因,我不确定该怎么做。
var resultTemplate = ({ result }) => {
return `
${result.id}
${result.relevantJobTypes.length < 1 ? '' : `
${result.relevantJobTypes.map(jobType => jobTypeTemplate({
jobType // , result.id -> I want to use result.id somehow in jobTypeTemplate
})).join('')}
`}
`;
}
var jobTypeTemplate = ({ jobType }) => {
// template code
}
更新
试图实现@ BERGI的答案,但该ID是内未定义relevantJobTypes
模板:
${result.relevantJobTypes.length < 1 ? '' : `
${result.relevantJobTypes.map(jobType => jobTypeTemplate({
jobType
}, result.id)).join('')}
`}
var jobTypeTemplate = ({ jobType, id }) => {
// template code
}
#2
终于明白了,我不得不将jobTypeTemplate
的签名更新为var jobTypeTemplate = ({ jobType }, id) => {
而不是jobType, id
这只是一个标准函数调用,与模板文字无关。 代替
jobTypeTemplate({
jobType, result.id // SyntaxError
})
您将需要使用
jobTypeTemplate({
jobType
}, result.id)
第二个论点。 当然,您也可以在对象文字中添加第二个属性:
jobTypeTemplate({
jobType,
id: result.id
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.