I am attempting to navigate when a button-click occurs in lightning. Nothing happens when I click the button. Any thoughts on how to make this work? Code sample included:
import { LightningElement, track } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class CreateNewTSSCaseButton extends
NavigationMixin(LightningElement) {
connectedCallback()
{
// Opening
console.log("connectedCallback.Start");
// Navigate to Account Page
this.accountHomePageRef = {
type: "standard__objectPage",
attributes: {
"objectApiName": "Account",
"actionName": "home"
}
};
this[NavigationMixin.GenerateUrl](this.accountHomePageRef)
.then(url => this.url = url);
// Closing
console.log("connectedCallback.Finish");
}
AddTSSCase_Click() {
// Opening
console.log("AddTSSCase_Click.Start");
// Perform navitation
console.log(this.accountHomePageRef);
this[NavigationMixin.GenerateUrl](this.accountHomePageRef)
.then(url => this.url = url);
// Closing
console.log("AddTSSCase_Click.Finish");
}
}
BTW, I am getting all Console.log messages in the Console window, so AddTSSCase_Click is firing.
Best Answer
You should either use
href
of anchor tag to go to url or you should useNavigate
method of mixin.Option 1: Using
Navigate
: (recommended)HTML:
JS:
Option 2: Using
href
:You have already stored
url
in connectedCallback. Now use it in HTML as below:JS: You have to make
url
tracked so that it reflect in HTML when it gets the value:ideally you should also use
catch
.