简体   繁体   English

获取错误:'无法分配到'location',因为它是一个常量或只读属性'与Angular app中的mailto函数

[英]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: 我正在尝试在我的Angular 2应用程序中设置一个功能,该功能将使用用户的默认电子邮件客户端发送一封电子邮件,其中包含一些预先填充的信息:

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. webpack:编译失败。

The examples I've seen so far all describe doing it thie way, with "window.location", so how can I resolve this issue? 我到目前为止看到的例子都描述了这样做,使用“window.location”,那么我该如何解决这个问题呢?

You're missing the href 你错过了href

window.location.href = ....

You can also do this with the Angular Router by giving it a static url: 您也可以通过为Angular Router提供一个静态URL来执行此操作:

this.router.navigateByUrl('url')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM