在 HTML 中将变量传递给 href

问题描述 投票:0回答:2
html web salesforce href lwc
2个回答
0
投票

你需要使用一些 Javascript :)

只需向您的 ancor 元素添加一个 id 属性:

<a id="anchor" href="https://develop.lightning.force.com/lightning/r/Contact//view">{doctor.Name}</a>

然后动态设置。

var a = document.getElementById('anchor');
var id = 0x323214343;
a.href = `https://develop.lightning.force.com/lightning/r/Contact/{{id}}/view`;

0
投票

在你的 lwc 的 JS 文件中,你可以为那个 url 公开一个 getter,它应该是相对的而不是绝对的:

get doctorURL() {
    // this is a relative url.
    return `/lightning/r/Contact/${this.doctor.Id}/view`;
}

我假设 Id 是

doctor
对象的属性。
然后在模板中你应该有:

<a href={doctorURL}>{doctor.Name}</a>

请注意,

document.getElementById();
在 LWC 中不起作用。
© www.soinside.com 2019 - 2024. All rights reserved.