Search code examples
javascriptangulartypescriptmailto

Getting error: 'Cannot assign to 'location' because it is a constant or a read-only property' with mailto function in Angular app


I am trying to set up a function in my Angular 2 app that will send an email using the user's default email client with some pre-populated info:

sendEmail() {
    this.title = document.title;
    this.title = this.title.replace("&", "-");
    window.location = "mailto:?body=" + this.title + " - " + window.location + "&subject=I thought this link might interest you.";
}

But I'm running into an issue where I'm getting an error:

Cannot assign to 'location' because it is a constant or a read-only property. webpack: Failed to compile.

The examples I've seen so far all describe doing it thie way, with "window.location", so how can I resolve this issue?


Solution

  • You're missing the href

    window.location.href = ....
    

    You can also do this with the Angular Router by giving it a static url:

    this.router.navigateByUrl('url')