如题,最近接到一个这样的需求。
原本通过lightning tab的link是长这个样子的:
/lightning/n/[myLightningComponentName]?0.source=alohaHeader
在它尾巴上加param后,重新访问,页面刷新后发现link上的参数已经无影无踪...
经过一番网上搜索,发现lightning 有“isUrlAddressable”这个特性。随后我做了一些探索后找到问题答案。如下:
1. lightning component需添加 “isUrlAddressable”
<aura:component implements="lightning:isUrlAddressable" controller="xxx"> ...
2. 这个component可通过这样的link访问:
/lightning/cmp/c__myLightningComponentAPIName?c__someParamName=xxxxx
注意:这里需要加上“c__"前缀, 参数前也要加,不加你拿不到,就是这么任性!(这是我掉进去的坑之一。 另我猜测,如果使用的lightning component有其他命名空间,”c__"需替换成相应的命名空间,未验证)。
补充,myLightningComponentAPIName这里是aura component的api name, 我再次试验的时候误填了对应的lightning component tab的api name,也不行。
3. 在lightning component的controller.js中,可以通过下列方法获取参数 (注意,这里同样参数需加"c__"前缀!不加拿不到,嘿嘿):
var myPageRef = component.get("v.pageReference");
var myWantedParamValue = myPageRef.state.c__someParamName;
OK 至此可通过link访问到lightning component 并愉快地获得参数 :)
补充-20210611: 今天发现,这货只能传一个参数,它妈的.