ionic4学习笔记(八)--Clipboard 将内容复制到剪贴板

1.ts文件中引入依赖

import { Plugins } from '@capacitor/core';

2.html文件中

<div>
        <ion-button (click)="clipboardSetString()">
            clipboardSetString
        ion-button>
    div>
    <div>
        <ion-button (click)="clipboardGetString()">
                clipboardGetString
        ion-button>
    div>
    <div>
        <ion-button (click)="clipboardSetURL()">
                clipboardSetURL
        ion-button>
    div>
    <div>
        <ion-button (click)="clipboardGetURL()">
                clipboardGetURL
        ion-button>
    div>

3. ts文件中写函数

clipboardSetString() {
    Plugins.Clipboard.write({
      string: "Hello, 123"
    });
  }

  async clipboardGetString() {
    let str = await Plugins.Clipboard.read({
      type: "string"
    });
    console.log('Got string from clipboard:', str.value);
  }

  clipboardSetURL() {
    Plugins.Clipboard.write({
      url: "http://google.com/"
    });
  }

  async clipboardGetURL() {
    let url = await Plugins.Clipboard.read({
      type: "url"
    });
    console.log("Get URL from clipboard", url.value);
  }

你可能感兴趣的:(ionic,混合APP)