美文网首页
uniapp复制到剪贴板(App、H5)

uniapp复制到剪贴板(App、H5)

作者: 宏_4491 | 来源:发表于2021-11-09 20:20 被阅读0次

    在utils 文件夹新建clipboard.js里面添加如下代码

    
    function copyTxt(txt) {//传入要复制的内容
        txt+="";
        if(txt=="null"||txt=="undefined"||txt==""){
            //toast("复制失败,内容为空");
            return;
        }
      // #ifdef APP-PLUS
      uni.setClipboardData({
          data:txt,
      });
      // #endif
     
      // #ifdef H5
      if (document.queryCommandSupported('copy')) {
          let textarea = document.createElement("textarea")
          textarea.value = txt
          textarea.readOnly = "readOnly"
          document.body.appendChild(textarea)
          textarea.select() // 选中文本内容
          textarea.setSelectionRange(0, txt.length) 
          let result = document.execCommand("copy") 
          textarea.remove()
          toast("复制成功");
      }else{
        toast("您的浏览器不支持复制");
      }
     
      // #endif  
    }
    export default {
        copyTxt,
    }
    

    如果是H5端可以使用插件的方式

    弊端:不支持h5
    
    2、安装vue-clipboard2 插件:
    
    npm install vue-clipboard2 --save
    
    安装完成后在man.js中引入:
    
    <pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; font-weight: 400; position: relative; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">import VueClipboard from 'vue-clipboard2' 
    Vue.use(VueClipboard);
    </pre>
    
    <pre style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; padding: 8px; font-weight: 400; position: relative; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">在需要的vue页面中调用:
    </pre>
    
    <text
       class="item-btn"
       v-clipboard:copy="item.spread_url"
       v-clipboard:success="(type) => onCopyResult('success')"
       v-clipboard:error="(type) => onCopyResult('error')">复制
    </text>
    
    onCopyResult(type) {undefined
         uni.showToast({undefined
              title: type==='success' ? '复制成功' : '复制失败',
              icon: 'none'
          })
    
    /**
     * 设置粘贴板数据
     * @param {String} text 要设置的字符串
     * 如果未设置参数,则清空数据
     */
    function setClipboardText(text){
        try{
        var os = plus.os.name;
        text = text||'';
        if('iOS' == os){
            // var UIPasteboard  = plus.ios.importClass('UIPasteboard');  
            // var pasteboard = UIPasteboard.generalPasteboard();  
            // pasteboard.setValueforPasteboardType(text, 'public.utf8-plain-text');
            var pasteboard = plus.ios.invoke('UIPasteboard', 'generalPasteboard');
            plus.ios.invoke(pasteboard, 'setValue:forPasteboardType:', text, 'public.utf8-plain-text');
        }else{
            var main = plus.android.runtimeMainActivity();
            // var Context = plus.android.importClass('android.content.Context');
            // var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
            var clip = main.getSystemService('clipboard');
            plus.android.invoke(clip, 'setText', text);
        }
        }catch(e){
            console.error('error @setClipboardText!!');
        }
    }
    
    function getClipboardText(){
        try{
        var os = plus.os.name;
        if('iOS' == os){
            var pasteboard = plus.ios.invoke('UIPasteboard', 'generalPasteboard');
            return plus.ios.invoke(pasteboard, 'valueForPasteboardType:', 'public.utf8-plain-text')
        }else{
            var main = plus.android.runtimeMainActivity();
            var clip = main.getSystemService('clipboard');
            return plus.android.invoke(clip, 'getText');
        }
        }catch(e){
            console.error('error @getClipboardText!!');
        }
    }
    
    
    module.exports = {
        setText: setClipboardText,
        getText: getClipboardText
    }
    

    相关文章

      网友评论

          本文标题:uniapp复制到剪贴板(App、H5)

          本文链接:https://www.haomeiwen.com/subject/dodvzltx.html