[SalesForce] Navigation in lightning out for LWC

I have a LWC which navigates to records using NavigationMixin. When I use the same LWC in VisualForce page using lightning out, the navigation does not work. I there any way to make it work? I did research but didn't find any guidance. Please help.

    navigateToRecordViewPage(event) {
        this.record = event.detail.row;
        // View a custom object record.
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: this.record.id,
                //objectApiName: 'Lead', // objectApiName is optional
                actionName: 'view'
            }
        });
    }

Best Answer

First check if you are in lightning or classic. If in lightning, use navigation.mixin. If in classic, use window.location.assign().

if(document.referrer.indexOf(".lightning.force.com") > 0){
            this[NavigationMixin.Navigate]({
                type: 'standard__recordPage',
                attributes: {
                    recordId: this.record.id,
                    actionName: 'view'
                }
            });
        }else{
            window.location.assign('/'+this.record.id);
        }
Related Topic