Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Navigator.share不是在Angular中工作,而是在codepen中的测试中工作_Javascript_Angular_Web_Share_Navigator - Fatal编程技术网

Javascript Navigator.share不是在Angular中工作,而是在codepen中的测试中工作

Javascript Navigator.share不是在Angular中工作,而是在codepen中的测试中工作,javascript,angular,web,share,navigator,Javascript,Angular,Web,Share,Navigator,我对navigator.share有问题 我在codepen中测试此代码: if (navigator.share) { navigator.share({ title: 'Web Fundamentals', text: 'Check out Web Fundamentals — it rocks!', url: 'https://developers.google.com/web', })

我对navigator.share有问题

我在codepen中测试此代码:

if (navigator.share) {
        navigator.share({
            title: 'Web Fundamentals',
            text: 'Check out Web Fundamentals — it rocks!',
            url: 'https://developers.google.com/web',
        })
          .then(() => console.log('Successful share'))
          .catch((error) => console.log('Error sharing', error));
      } else {
        console.log('no share');
      }
}

在我的移动设备工作

但是当我把这个代码放在我的应用程序中时,Angular不会进入if

我还测试了canShare方法,因为这是一个测试,但我想要共享图像。使用标记保留在DOM中的图像,但这是另一个问题

问题是我可以在codepen中共享任何东西,但不能在Angular应用程序中共享

为了在angular中使用navigator,我必须将以下代码放在navigator之前:

let varNavigator: any;

varNavigator = window.navigator; 
if (varNavigator.share) {
 ...
如果我直接执行varNavigator.share,则显示此错误:

core.js:1427 ERROR TypeError: (intermediate value)(intermediate value).share is not a function
    at e._next (home.component.ts:299)
    at e.__tryOrUnsub (Subscriber.js:239)
    at e.next (Subscriber.js:186)
    at e._next (Subscriber.js:127)
    at e.next (Subscriber.js:91)
    at e.notifyComplete (ForkJoinObservable.js:197)
    at e._complete (InnerSubscriber.js:32)
    at e.complete (Subscriber.js:116)
    at e._complete (Subscriber.js:134)
    at e.complete (Subscriber.js:116)
我从URL获取代码:

你可以试试这个

const navigator = window.navigator as any;

if (navigator.share) {
  navigator
    .share({
      title: 'Google',
      text: 'Save',
      url: 'https://google.com'
    })
    .then(() => console.log('Successful share'))
    .catch(error => console.log('Error sharing', error));
} else {
  alert('share not supported');
}

还有。。。请确保您在https(或本地主机)上交付站点。

您可以在e.U next(home.component.ts:299)发布代码吗?第299行是一条注释。。。我不明白。我删除了这段代码,现在同样的错误e.(下一行代码显然是正确的)。。。我相信,作为一个已编译的代码,这一行指示的代码是不正确的。打开开发工具并单击“错误”,它应该会将您抛出这一行,这就是我所做的