2017-12-15 13 views
2

私のアプリケーションでWebShareAPIを使用する必要があります。プロパティ '共有'がタイプ 'ナビゲータ'に存在しません

ここ

Introducing the Web Share 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'); 
} 

で提案されているように私のコードですが、活字体のコンパイルは次のエラーで失敗します。

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

そうな理由がありますが、ここで DOM lib: add support for navigator.share

を説明私は、WebShareApiを自分のIonicアプリで動作させるいくつかの回避策を知りたいular、Angular、Typescriptのいずれのアプリでも使用できます。

答えて

4

answer、 に基づいて、タイプanyの変数を定義し、タイプナビゲータの値を割り当てようとすることができます。発行はtypeScriptの入力に関連しています。

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'); 
} 

もう1つのオプションは、上に掲載したリンクで提案されているように、インターフェイスナビゲータを拡張することです。

+0

は、あなたは、単に[グローバル](https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-d-tsを使用することはできません私のために働きました。 html)? –

+0

@FlavienVolken、あなたはglobals.d.tsファイルに新しいインタフェースタイプを追加することを意味しますか?変数 'navigator.share'をプロジェクトの異なるレベルで使用する場合は、それが良い選択です。そうでなければ、解答で説明するように進みます。 – edkeveked

0

これは

let newVariable = (window.navigator as any) 
if(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'); 
} 
関連する問題