隨着salesforce對lightning的推進,越來越多的項目基於lightning開發,導致很多小伙伴可能都並不了解classic或者認為不需要用到classic直接就開始了lightning的開發。其實有精力了解classic的使用還是很有必要的,因為lightning還在不斷的優化中,可能有一部分還需要使用classic的功能來實現或者來協助實現,比如list view的list button目前只能使用visualforce page搭配lightning component。那么vf 如何去引用已經弄好的lightning component呢,我們接下來使用一個demo去簡單了解一下。
需求:在lightning環境下的contact list view定義一個自定義的list button,實現使用pop up方式彈出所勾選的數據列表( lwc + aura實現)。
實現步驟:
1.構建LwC component畫UI;
2. 構建aura component包含lwc component;
3. 創建aura single APP繼承ltng:outApp(包含SLDS樣式庫)/ltng:outAppUnstyled(不包含SLDS樣式庫),使用aura:dependency標簽的resource屬性引入2步驟中的aura component;
4. 創建vf page,使用$Lightning.use引入上面的aura single APP,然后動態創建component顯示即可。
Talk is cheap,show me the code.下面根據上面的需求進行開發。
1. ContactListController.cls:根據選擇的contact id list進行搜索數據,因為前端使用wire裝載方式,所以方法聲明必須使用cacheable=true
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的功能。這里需要有幾點小小的注意:
- 需要設置recordSetVar,這樣才可以使用到list view的list button中;
- 需要引入apex:includeLightning,最好放在引入的第一行;
- javascript中使用GETRECORDIDS函數來獲取列表中選擇的數據選項,在vf page中需要使用{!selected}來獲取,因為在js中如果使用''方式擴上他返回的是string類型,不擴上直接在list引用會報錯,所以這里使用apex:repeat方式將其迭代在一個list中;
- 使用$lightning.use引入一個single app,然后在動態創建里面的auraDependency的component,$lightning.use可以多次使用,但是需要多次引入不同的single app,詳細的使用自行查看文檔。
<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知識以及限制自行查看。篇中有錯誤地方歡迎指出,有不懂地方歡迎留言。