Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 790
Muirik Avatar asked Jun 14 '17 19:06

Muirik


1 Answers

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')
like image 169
joh04667 Avatar answered Oct 23 '22 15:10

joh04667