javascript - Vuejs 获取组件 HTML 并通过 ajax POST 发送
问题描述
我想发送带有 HTML 正文的电子邮件,但我无权访问服务器代码,我只能通过 ajax 发送电子邮件内容。服务器已经有 API 来发送电子邮件。
我有invoiceEmail
即时渲染的组件。
sendEmail () {
const mailConfig = {
to : 'johndoe@example.com',
subject : 'Your Invoice',
// body : this.$refs.invoiceEmail.$el, // this doesn't work
// I'm getting error "converting circular structure to json"
body : '<strong style="color:red;">red bold</strong>', // this works
}
this.$api.POST('send-email', mailConfig)
.then(response => {})
.catch(error => {})
.finally(() => {})
},
}
如果我将 HTML 填充body
为 string: '<strong>this should be bold</strong>'
,它会按预期工作。所以如果我能得到 HTML,我相信代码会起作用。
这是console.log(this.$refs.invoiceEmail.$el)
我应该使用什么来获取纯 HTML 而不是this.$refs.invoiceEmail.$el
??
解决方案
您可以this.$refs.yourRef.$el.innerHTML
将内部 HTML 作为字符串返回。
也只是稍微扩展一下这个答案:this.$refs.yourRef.$el
返回一个原始 DOM 对象,就像您document.getElementById('myelement')
使用原始 Javascript 一样。
推荐阅读
- java - 作为参数传递的类/活动实例在将其分配给类范围的变量并使用它时为空
- php - 使用 php 转发电子邮件时出错
- javascript - 使用 react-datepicker 仅在选定的 div 中选择和更新日期
- c# - 使用 C# 的 Azure REST Api 身份验证
- typescript - EmptyError:在角度保护中使用 takeWhile 时没有序列中的元素
- html - 在Angular 6中折叠/展开嵌套的div
- mdx - MDX 将集合除以度量
- solr - 使用 Solr 自定义预输入
- bash - 如何在 sed 中处理 UTF-8 表情符号?
- javascript - 如何在按钮按下时触发警报功能?