“导航器"类型上不存在属性“共享" [英] Property 'share' does not exist on type 'Navigator'

查看:24
本文介绍了“导航器"类型上不存在属性“共享"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 Ionic 应用程序中使用 WebShareAPI.

I need to use WebShareAPI in my Ionic application.

这是我在介绍网络共享 API

if (window.navigator && window.navigator.share) {
  window.navigator.share({
    title: 'title',
    text: 'description',
    url: 'https://soch.in//',
  })
    .then(() => console.log('Successful share'))
    .catch((error) => console.log('Error sharing', error));
} else {
  alert('share not supported');
}

然而,Typescript 编译失败并出现以下错误:

However, Typescript compilation fails with the following error:

[11:31:11]  typescript: src/pages/channel_home/channel_home.ts, line: 89
            Property 'share' does not exist on type 'Navigator'.

这里解释了一个可能的原因DOM 库:添加对 navigator.share 的支持

There is a likely reason explained here DOM lib: add support for navigator.share

但是,我想知道一些解决方法,使 WebShareApi 特别适用于我的 Ionic 应用程序,以及一般的任何 Angular 或 Typescript 应用程序.

However, I would like to know some workaround to make WebShareApi work in my Ionic app in particular, and in any Angular or Typescript app in general.

推荐答案

基于此 回答,您可以尝试定义一个 any 类型的变量,并将您的 Type Navigator 值分配给它.该问题与 typeScript 类型有关.

Based on this answer, you can try to define a variable of type any and assign to it your value of Type Navigator. The isssue is related to typeScript typings.

let newVariable: any;

newVariable = window.navigator;

if (newVariable && newVariable.share) {
  newVariable.share({
    title: 'title',
    text: 'description',
    url: 'https://soch.in//',
  })
    .then(() => console.log('Successful share'))
    .catch((error) => console.log('Error sharing', error));
} else {
  alert('share not supported');
}

另一种选择是扩展界面导航器,正如我在上面发布的链接中所建议的那样.

Another option would be to extend the interface Navigator as it is suggested in the link I posted above.

这篇关于“导航器"类型上不存在属性“共享"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆