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

1

11

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?

Johnny answered 14/6, 2017 at 19:17 Comment(1)
Possible duplicate of #13107450Pigment
H
19

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')
Harwin answered 14/6, 2017 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.