iview复制内容到粘贴板(在哪复制都通用)

版权声明:未经本人同意不得转载 一切法律责任 后果自负 https://blog.csdn.net/xy19950125/article/details/84111386
 {
                        title:"渠道连接",
                        key:"channelUrl",
                        minWidth: 250,
                        align: "center",
                        render: (h, params) => {
                            return h("div",[
                                h(
                                    "span",
                                    params.row.channelUrl
                                ),
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "primary",
                                            size: "small",
                                        },
                                        on: {
                                            click: () => {
                                               const copyToClipboard = str => {
                                                    const el = document.createElement('textarea'); // Create a <textarea> element
                                                    el.value = str; // Set its value to the string that you want copied
                                                    el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
                                                    el.style.position = 'absolute'; 
                                                    el.style.left = '-9999px'; // Move outside the screen to make it invisible
                                                    document.body.appendChild(el); // Append the <textarea> element to the HTML document
                                                    const selected = 
                                                    document.getSelection().rangeCount > 0 // Check if there is any content selected previously
                                                    ? document.getSelection().getRangeAt(0) // Store selection if found
                                                    : false; // Mark as false to know no selection existed before
                                                    el.select(); // Select the <textarea> content
                                                    document.execCommand('copy'); // Copy - only works as a result of a user action (e.g. click events)
                                                    document.body.removeChild(el); // Remove the <textarea> element
                                                    if (selected) { // If a selection existed before copying
                                                    document.getSelection().removeAllRanges(); // Unselect everything on the HTML document
                                                    document.getSelection().addRange(selected); // Restore the original selection
                                                    }
                                                };
                                                copyToClipboard(params.row.channelUrl)
                                            }
                                        }
                                    },
                                    "复制"
                                ),

                            ])
                        }
                    },

上面是定义好的一个方法  不管在哪里 都是通用的

猜你喜欢

转载自blog.csdn.net/xy19950125/article/details/84111386