Javascript 使用特定对象动态插值字符串
我有一个组件,其中有一个对象列表,我希望传入一个输入参数,该参数描述在单击任何特定行时应该导航到的路由 如下所示:Javascript 使用特定对象动态插值字符串,javascript,angular,Javascript,Angular,我有一个组件,其中有一个对象列表,我希望传入一个输入参数,该参数描述在单击任何特定行时应该导航到的路由 如下所示: //Some sort of object may have an 'id' field in some cases (but not in other cases... if it didn't I would pass in a different string below) @Input() listData: any[]; @Input()
//Some sort of object may have an 'id' field in some cases (but not in other cases... if it didn't I would pass in a different string below)
@Input()
listData: any[];
@Input()
link: string; // Input of something like "/List/Detail/${row.id}"
rowClick(row: any){
//Grab row.id and interpolate it so result is "/List/Detail/1"
???
router.navigate([url]);
}
看起来应该很简单,但我很难把我的头绕在它周围
这里的想法是,我的列表项有时会链接到一个页面,有时会链接到另一个页面,具体取决于“link”参数。使用lodash
\uz.template
:
const compiledTemplate=\模板(this.link);
编译模板({row});
使用Javascript:
constlinkparts=this.link.split('/');
linkParts.pop();
linkParts.push(row.id);
const linkWithRowId=linkParts.join('/');
您可以使用lodash u.template const url=${row.url}/${row.id}
@ChristopherMarshall您必须删除${row.id}部分btwI我不确定Obj的结构,所以只需将示例放在字符串文字中。我已经在项目中使用了lodash,所以我将尝试一下。看起来应该有用。谢谢