【发布时间】:2021-07-13 12:40:30
【问题描述】:
在我的 Nuxt PWA 中,我有一个使用 this package 将我的 HTML 转换为 Canvas 的函数。生成的图像以 64 为基数。现在我希望能够通过以下方式共享该图像:Whatsapp、Facebook、电子邮件、Instagram 等。我找到了几个包,但它们似乎都不支持仅共享文件 URL 和文本。
这是我的分享功能:
shareTicket(index) {
html2canvas(this.$refs['ticket-' + index][0], {
backgroundColor: '#efefef',
useCORS: true, // if the contents of screenshots, there are images, there may be a case of cross-domain, add this parameter, the cross-domain file to solve the problem
}).then((canvas) => {
let url = canvas.toDataURL('image/png') // finally produced image url
if (navigator.share) {
navigator.share({
title: 'Title to be shared',
text: 'Text to be shared',
url: this.url,
})
}
})
当我取出 if (navigator.share) 条件时,我在控制台中收到错误消息,指出 navigator.share 不是函数。我在某处读到它只适用于 HTTPS,所以我上传到我的登台服务器并尝试过,但仍然遇到同样的错误。
为了清楚起见,我希望能够共享生成的图像本身而不是 URL。
【问题讨论】:
-
如果你把这些都写在
if (process.client) { // insert your whole code here }会发生什么?还有,这个好像主要用在手机上,你是在手机上试过还是只在桌面上用过? -
抱歉,我不知道 process.client 是什么意思,如果同时适用于移动设备和桌面设备,我已经在两者上都尝试过了,但它不起作用。
-
这是来自 Nuxt 文档,基本上是说代码不应该在服务器上运行。 nuxtjs.org/docs/2.x/internals-glossary/context 按照 MDN 的文档,我实现了使演示正常工作:developer.mozilla.org/en-US/docs/Web/API/Navigator/share 你想要和这个页面一样的结果,对吧?
-
当我将代码包装在
if (process.client)中时,我仍然收到错误navigator.shareis not a function。不知道它不能与服务器一起使用,不知道如何运行我的应用程序。是的,我希望能够分享您分享的链接中描述的文件。 -
对不起什么包?我很困惑。
标签: javascript vue.js nuxt.js html2canvas web-share