0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VueUse クリップボード

Last updated at Posted at 2025-08-29

VueUse で クリップボード機能を使う。

単純に、useClipboard()関数を使えば出来てしまいます。

コード

コードペン用のコードなので、ちょっと書き方が変かもしれません。
(コードペンでVue(とVueUse)を使う前回の記事を参照)

前回のマウス座標取得コードも残してます。

js
const { createApp, ref } = Vue;
const { useMouse } = VueUse;
const { useClipboard } = VueUse;


const app = createApp({
  setup() {    
    const { x, y } = useMouse();
    const source = ref("");
    const { text, copy, copied } = useClipboard({ source });
    
   return { x, y, source,text, copy, copied };

  },
});
app.mount('#app');
html
<div id="app" >
   マウス座標: {{ x }}, {{ y }}
  
  <div>
  <input v-model="source" />
    <button @click="copy(source)">コピー</button>
      <p>コピーしたテキスト: {{ text }}</p>
  </div>
</div>

動作

テキストボックスに「あいうえお」と書いて、コピーボタンを押すと、
クリップボードにあいうえおがコピーされます。

11.png

適当なテキストエディタを開いて、
貼り付けを押すと、「あいうえお」が貼り付けられました!

スクリーンショット 2025-08-28 200406.png

コードペン

See the Pen VueUse useClipboard by sasuke (@vhmbdiog-the-flexboxer) on CodePen.

このQiita埋め込みのコードペン上では動作しない模様?
ちゃんとコードペンのサイトに行かないとダメかもしれません。
(右上の「EDIT ON CODEPEN」から行けます)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?