随着salesforce对lightning的推动,愈来愈多的项目基于lightning开发,致使不少小伙伴可能都并不了解classic或者认为不须要用到classic直接就开始了lightning的开发。其实有精力了解classic的使用仍是颇有必要的,由于lightning还在不断的优化中,可能有一部分还须要使用classic的功能来实现或者来协助实现,好比list view的list button目前只能使用visualforce page搭配lightning component。那么vf 如何去引用已经弄好的lightning component呢,咱们接下来使用一个demo去简单了解一下。javascript
需求:在lightning环境下的contact list view定义一个自定义的list button,实现使用pop up方式弹出所勾选的数据列表( lwc + aura实现)。html
实现步骤:前端
1.构建LwC component画UI;java
2. 构建aura component包含lwc component;web
3. 建立aura single APP继承ltng:outApp(包含SLDS样式库)/ltng:outAppUnstyled(不包含SLDS样式库),使用aura:dependency标签的resource属性引入2步骤中的aura component;api
4. 建立vf page,使用$Lightning.use引入上面的aura single APP,而后动态建立component显示便可。app
Talk is cheap,show me the code.下面根据上面的需求进行开发。函数
1. ContactListController.cls:根据选择的contact id list进行搜索数据,由于前端使用wire装载方式,因此方法声明必须使用cacheable=truefetch
public with sharing class ContactListController { @AuraEnabled(cacheable=true) public static List<Contact> fetchContactListByIDs(List<String> idList){ return [SELECT Id,Name FROM Contact WHERE Id IN :idList]; } }
2. contactListForLwc.html:用来展现一个popup modal,modal中展现一个table数据优化
<template> <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open"> <div class="slds-modal__container"> <!-- modal header start --> <header class="slds-modal__header"> <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse" title="Close"> <lightning-icon icon-name="utility:close" alternative-text="close" variant="inverse" size="small" ></lightning-icon> <span class="slds-assistive-text">Close</span> </button> <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">Selected Contact List</h2> </header> <!-- modal body start --> <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1"> <table class="slds-table slds-table_cell-buffer slds-table_bordered"> <thead> <tr class="slds-line-height_reset"> <th class="" scope="col"> <div class="slds-truncate">Contact Id</div> </th> <th class="" scope="col"> <div class="slds-truncate">Contact Name</div> </th> </tr> </thead> <tbody> <template if:true={contactList.data}> <template for:each={contactList.data} for:item="contact"> <tr key={contact.Id}> <td>{contact.Id}</td> <td> {contact.Name}</td> </tr> </template> </template> <template if:false={contactList.data}> <tr> <td colspan="2">List View is not contains any data</td> </tr> </template> </tbody> </table> </div> </div> </section> <div class="slds-backdrop slds-backdrop_open"></div> </template>
contactListForLwc.js:调用后台获取列表
import { LightningElement, api, wire } from 'lwc'; import fetchContactListByIDs from '@salesforce/apex/ContactListController.fetchContactListByIDs'; export default class ContactListForLwc extends LightningElement { @api contactIdList; @wire(fetchContactListByIDs,{idList:'$contactIdList'}) contactList; }
3. ContactListForAura.cmp:用于包一层lwc,用来在single app中使用,由于目前的动态建立component只能aura,因此lwc须要套一层。
<aura:component> <aura:attribute name="selectedIds" type="List" default="" /> <c:contactListForLwc contactIdList="{!v.selectedIds}"/> </aura:component>
4. ContactListApp.app:建立single app,设置access 为GLOBAL,由于须要使用SLDS的样式,这里extends为ltng:outApp,而后经过aura:dependency引入想要渲染的子component
<aura:application access="GLOBAL" extends="ltng:outApp"> <aura:dependency resource="c:ContactListForAura"/> </aura:application>
5. ContactListPage.page:用于声明contact list类型,而后使用$Lightning.user实现lightning out的功能。这里须要有几点小小的注意:
<apex:page standardController="Contact" recordSetVar="Contacts" showHeader="false"> <apex:includeLightning/> <div id="lightning" /> <script> var selectedList = []; </script> <apex:repeat value="{!selected}" var="selectedItem"> <script> selectedList.push('{!selectedItem}'); </script> </apex:repeat> <script> if(selectedList.length == 0) { window.location.href = '/003'; } else { $Lightning.use("c:ContactListApp", function() { $Lightning.createComponent("c:ContactListForAura", {selectedIds : selectedList}, 'lightning', function(cmp) { console.log("component created"); } ); }); } </script> </apex:page>
6. 建立contact的list button,而后类型选择 list button,选择指定的vf page,而后在search layout中的list view中将定义的拖拽便可。
效果展现:
1.Contact列表勾选了两条数据,而后点击按钮
2. 弹出页面展现选择的两条数据。
总结:篇中经过简单的例子展现了lightning out实现以及list view button关于vf page如何引入lightning component / lightning web component。缺点是使用vf page没法实现相似action的效果在本页pop up,查找了不少资料也没有实现,有好的实现方式欢迎留言。lightning out实际场景不单单demo中的使用场景,详细的lightning out知识以及限制自行查看。篇中有错误地方欢迎指出,有不懂地方欢迎留言。