Uncaught SyntaxError: Unexpected identifier - Webshare api

Uncaught SyntaxError: Unexpected identifier - Webshare api

我正在尝试在 Jekyll 帖子页面中实现 api of android chrome 的网络共享。下面是我的代码。

<script>
  document.querySelector('share-btn2').addEventListener('click', function() {
    console.log('clicked');
    if(navigator.share) {
      navigator.share({
        title: {{ page.title }},
        text: {{ page.content }},
        url: {{ site.url }}{{ page.url }}
      })
      .then(() => console.log('Success'))
      .catch((error) => console.log('Error sharing', error));
    }

  });

</script>

但是我在第 title: {{ page.title }}, 行的控制台中收到 Uncaught SyntaxError: Unexpected identifier 错误。请更正我的代码。谢谢。

共享属性的值本质上是字符串,而不是您提供的值。这是来自 Mozilla 文档:

    var sharePromise = window.navigator.share(data);

data = An object containing data to share. At least one of the following fields must be specified. Available options are:

url: A USVString representing a URL to be shared.
text: A USVString representing text to be shared.
title: A USVString representing the title to be shared.


    navigator.share({
      title: document.title,
      text: 'Hello World',
      url: 'https://developer.mozilla.org',
    }); // share the URL of MDN

你的意思是:

  navigator.share({
    title: page.title,
    text: page.content,
    url: site.url + page.url
  });

您的 javascript 代码似乎没有被 Jekyll 处理。

请务必在您要处理的任何文件中设置 front matter

---
# even an empty front matter is ok
---
<script>
  document.querySelector('share-btn2').addEventListener('click', function() {
    console.log('clicked');
    if(navigator.share) {
      navigator.share({
        title: '{{ page.title }}',
        text: '{{ page.content }}',
        url: '{{ site.url }}{{ page.url }}'
      })
      .then(() => console.log('Success'))
      .catch((error) => console.log('Error sharing', error));
    }

  });

</script>