Salesforce Interview Questions & Answers Book is now available!

Grab Now

LWC

Navigate user from LWC to an external URL

Let's look at how can we navigate the user from LWC to an external URL.

By Teja, ex-Salesforce engineer · Updated April 20, 2021 · 1 min read

Very rarely you might have to navigate the user from Lightning Web Components to an external URL. Out of the box, Salesforce provides a solution to that.

Let us look at a snippet on that.

import { LightningElement } from "lwc";

import { NavigationMixin } from "lightning/navigation";

export default class ExploreNavigation extends NavigationMixin(
  LightningElement
) {
  handleNavigate() {
    const config = {
        type: 'standard__webPage',
        attributes: {
            url: 'http://salesforcecasts.com'
        }
	};
    this[NavigationMixin.Navigate](config);
  }
}

Navigating a user to an external URL from LWC

The approach that we will be following is going to be as usual. We need to create a config object and pass it to the method this[NavigationMixin.Navigate].

In the config that we provide, we need to specify the type as standard__webpage and in attributes, we need to specify the external URL, the rest of everything will be taken care by the platform.

Hope that was helpful!


Tip

Grab your FREE course

I have put together a FREE course on Javascript that helps you master Lightning Web Components.

It just takes 2 hours to finish the course and you will be able to feel the difference.

JavaScript for LWC →

Learn it by building it

Every course, every code repository, the discussion group and certificates of completion — one subscription.

Keep me in the loop!

Email me when a new episode is published (promise, I will not spam you)

You're signing up to receive emails from Salesforce Casts