【问题标题】:Call lightning Web Component from URL and capture parameter from URL从 URL 调用 Lightning Web 组件并从 URL 捕获参数
【发布时间】:2022-01-05 16:19:44
【问题描述】:

我想通过单击体验云中的 URL 将用户从一个 LWC 重定向到另一个。基本上,在我的第一个 LWC 上,我显示从 Apex 检索到的记录列表,然后当单击任何行时,我想传递该行的 recordId 并重定向到第二个 LWC,它将显示记录详细信息页面。我怎样才能做到这一点?

【问题讨论】:

  • 您需要第二个 LWC 吗?您可以改用标准记录详细信息页面吗?

标签: salesforce apex lwc salesforce-communities


【解决方案1】:

您是否有社区...抱歉,带有目标 LWC 的体验生成器页面已删除?

您可以使用 NavigationMixin,尽管文档说社区不支持 state 属性。

在源端尝试类似的东西

/* Assumes the link/button clicked looks like
    <button data-id={acc.Id} onclick={handleClick}>Get details</button>
*/
handleClick(event){
    if(event.currentTarget.dataset.id){
        this[NavigationMixin.Navigate]({
            type: 'comm__namedPage',
            attributes: {
                name: 'MyTargetCommunityPage__c'
            },
            state: {
                id: event.currentTarget.dataset.id
            }
        });
    }
}

然后在目标页面的 LWC 上应该可以访问为

connectedCallback() {
    if(window.location.href){
        try{
            let url = new URL(window.location.href);
            var id = url.searchParams.get('id');
            if(id){
                this.myId = id;
            }
        } catch(e){
            if(console){
                console.log(JSON.stringify(e));
            }
        }
    }
}

如果导航给您带来困难,您可以放弃它并直接使用window.open('/pagename?id=123', _self); 或其他东西。你有一套核心 Salesforce 规则,一套社区规则,这有点烦人。

(如果您有命名空间 - 您可能需要在 state 中使用 myns__id 而不是 id

【讨论】:

  • 我也使用了 state 属性并且它有效,但我不知道为什么文档有这一点。
猜你喜欢
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
相关资源
最近更新 更多