javascript - 获取错误 : 'Cannot assign to ' location' because it is a constant or a read-only property' with mailto function in Angular app

标签 javascript angular typescript mailto

我正在尝试在我的 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.";
}

但是我遇到了一个错误:

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

到目前为止我看到的例子都描述了如何使用“window.location”,那么我该如何解决这个问题呢?

最佳答案

您缺少 href

window.location.href = ....

您也可以通过给 Angular Router 一个静态 url 来做到这一点:

this.router.navigateByUrl('url')

关于javascript - 获取错误 : 'Cannot assign to ' location' because it is a constant or a read-only property' with mailto function in Angular app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552984/

相关文章:

javascript - 在 Ember Js 组件中定义转换 To

javascript - 在 React Native 中滑动背景图像

angular - emitDecoratorMetadata 及其在转译代码中的重要性

javascript - 打印Ionic2页面中的html内容

javascript - 从 TypeScript 扩展数组

javascript - React.js - 如何在子组件中实现一个函数,以从同一父组件中卸载另一个子组件,并在其所在位置安装另一个组件?

javascript - 我需要使用app.quit()关闭所有窗口,但由于某些窗口被隐藏而无法正常工作

javascript - 嵌入式脚本不适用于 Angular 7

angular - 检测 FormArray 子项的变化

node.js - TypeScript Node.js 该命令未定义