我们可以使用 aura+lwc的方法来实现, 将aura和chatbot放到同一个page上,在aura在检测到url发生变化时,向messageChannel中存储数据,lwc进行捕捉调用Minimized的方法即可
code
aura.cmp
<aura:component implements="forceCommunity:availableForAllPageTypes,flexipage:availableForAllPageTypes"> <aura:handler event="forceCommunity:routeChange" action="{!c.handleRouteChange}"/> <lightning:messageChannel type="test_Minimized__c" aura:id="test_Minimized__c"/> </aura:component>
aura.controller
({ handleRouteChange : function(component, event, helper) { console.log(window.location.href); let urlJson = window.location.href; if (urlJson.includes('/s/test') ){ var payload = { isMinimizedChat: true }; component.find("test_Minimized__c").publish(payload); } } });
lwc html
<template> <!-- <img src="path_to_your_logo" alt="My Logo"></img> --> <h2 aria-live="polite"> {text} </h2> <button onclick={minimize} aria-live="off" id="miniId"> <lightning-icon icon-name="utility:minimize_window"></lightning-icon> </button> <button onclick={close} aria-live="off"> <lightning-icon icon-name="utility:close"></lightning-icon> </button> </template>
lwc js
``import { wire } from ‘lwc’;
import BaseChatHeader from ‘lightningsnapin/baseChatHeader’;
import { subscribe, MessageContext } from ‘lightning/messageService’;
import MinizedChannel from ‘@salesforce/messageChannel/test_Minimized__c’;
export default class Etp_ChatHeader extends BaseChatHeader {
@wire(MessageContext)
messageContext;
subscription = null;
// Encapsulate logic for Lightning message service subscribe and unsubsubscribe
subscribeToMessageChannelMinizied() {
if (!this.subscription) {
this.subscription = subscribe(
this.messageContext,
MinizedChannel,
(message) => this.handleMessage(message),
// { scope: APPLICATION_SCOPE }
);
}
}
// Handler for message received by component
handleMessage(message) {
console.log('url has been changed');
this.minimize();
}
/**
* Text to display in h2 element.
* @type {string}
*/
text;
/**
* Set handlers for events from the sidebar.
*/
connectedCallback() {
this.subscribeToMessageChannelMinizied();
}
setText(str) {
if (typeof str !== "string") {
throw new Error("Expected text value to be a `String` but received: " + str + ".");
}
this.text = str;
}
}
``