3

When i try to navigate to a webpage it's loading in a new page. I want to replace current page url with the navigation URL. It' not working with below code. can anyone tell how to replace current page????

<template>
    <button onclick={navigateToWebPage}>Click To Navigate</button>
</template>

import { LightningElement, api, track } from 'lwc';
import { NavigationMixin } from "lightning/navigation";
export default class SelectComponent extends NavigationMixin(LightningElement) {
navigateToWebPage() {
    // Navigate to a URL
    this[NavigationMixin.Navigate]({
        type: 'standard__webPage',
        attributes: {
            url: 'http://salesforce.com'
        }
    },
    true // Replaces the current page in your browser history with the URL
  );
}
}
0

You can achieve like below,

parentLWCComponent.html

Template

<template>
        <lightning-card >
            <lightning-button label="The Parent LWC" onclick={navigateToChildLWC}></lightning-button>
        </lightning-card><br/>
</template>

parentLWCComponent.js file

import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class ParentPage extends NavigationMixin(LightningElement) {

    navigateToChildLWC() {
    this[NavigationMixin.Navigate]({
        "type": "standard__component",
        "attributes": {
            "componentName": "c__AuraComponent"    
        }
    });
}

Create an aura component as below to call the child LWC component from the Parent LWC component.

AuraComponent.cmp

<aura:component  implements="lightning:isUrlAddressable" access="global" >
   <c:childLWCComponent/>
</aura:component>

Crete your child LWC component,

childLWCComponent.html

<template>
    <h1>Hello World! </h1>
</template>
0
//Navigate to WebPage
navigateToWebPage() {
    this[NavigationMixin.GenerateUrl]({
        type: 'standard__webPage',
        attributes: {
            url: ''
        }
    }).then(url => {
        window.open("http://salesforce.com", "_self");
    });
}
0

I had the same problem with NavigationMixin not replacing the current page with the navigation URL. I used JS as a workaround here, see below, it is working for me.

window.location.replace("https://www.google.com/");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.