개발이야기/Vue - Vuetify

자바스크립트 클립보드 복사 안될 때

라울이 2021. 1. 3. 16:23

 

textCopy: function(text) {
    return new Promise(function(resolve, reject) {
        if (text) {
            const txt = document.createElement("textarea");
            document.body.appendChild(txt);
            txt.value = text; // chrome uses this
            let sel = getSelection();
            let range = document.createRange();
            range.selectNode(txt);
            sel.removeAllRanges();
            sel.addRange(range);
            if (document.execCommand("copy")) {
            console.log("copied");
            }
            document.body.removeChild(txt);
            alert("복사 되었습니다.\n붙여넣기(ctrl+v)로 사용하세요.");
            resolve();
        } else {
            reject(Error("복사 실패"));
        }
    });
},