Nesting template strings in Typescript

岁酱吖の 提交于 2019-12-11 06:57:20

问题


I'm trying to retrieve a variable from an object.

cell: (row: any) => `${row.testcolumn}`

My only issue is that I don't know what 'testcolumn' is ahead of time as I'm doing this dynamically. I'm not sure what to do, and the nested template string strategy I'm attempting will not compile.

cell: (row: any) => `${row.(`${varString}`)}`

I've also tried just using the variable name instead of nesting a template string, but that just looks for the varString value in the object, which doesn't exist. Is there any way I can use a nested literal to substitute the string value into the template literal and it still look for row.testcolumn instead of row.varString?


回答1:


It is same for template literals as for regular JS. Object properties can be retrieved dynamically with bracket notation, row[varString].

It will be:

cell: (row: any) => `${row[varString]}`


来源:https://stackoverflow.com/questions/48796938/nesting-template-strings-in-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!