使用vueuse实现在dialog中copy功能

This commit is contained in:
不做码农 2022-06-05 18:07:31 +08:00
parent 61d1279200
commit 43743ebe54
2 changed files with 22 additions and 15 deletions

View File

@ -173,9 +173,9 @@
<el-col :lg="24"> <el-col :lg="24">
<el-form-item label="访问路径"> <el-form-item label="访问路径">
{{ formView.accessUrl }} {{ formView.accessUrl }}
<!-- <el-button class="copy-btn-main" icon="document-copy" text @click="copyText(formView.accessUrl)"> <el-button class="copy-btn-main" icon="document-copy" text @click="copyText(formView.accessUrl)">
{{ $t('btn.copy') }} {{ $t('btn.copy') }}
</el-button> --> </el-button>
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col :lg="24"> <el-col :lg="24">
@ -188,7 +188,7 @@
</template> </template>
<script setup name="sysfile"> <script setup name="sysfile">
import { listSysfile, delSysfile, getSysfile } from '@/api/tool/file.js' import { listSysfile, delSysfile, getSysfile } from '@/api/tool/file.js'
import useClipboard from 'vue-clipboard3' import { useClipboard } from '@vueuse/core'
// id // id
const ids = ref([]) const ids = ref([])
// //
@ -346,12 +346,15 @@ function submitUpload() {
} }
proxy.$refs.uploadRef.submitUpload() proxy.$refs.uploadRef.submitUpload()
} }
const { toClipboard } = useClipboard()
const { copy, isSupported } = useClipboard()
const copyText = async (val) => { const copyText = async (val) => {
try { if (isSupported) {
await toClipboard(val) copy(val)
proxy.$message.success('复制成功!') proxy.$modal.msgSuccess('复制成功!')
} catch (e) {} } else {
proxy.$modal.msgError('当前浏览器不支持')
}
} }
handleQuery() handleQuery()
</script> </script>

View File

@ -62,9 +62,7 @@
<el-dialog :title="preview.title" v-model="preview.open" width="80%" top="5vh" append-to-body> <el-dialog :title="preview.title" v-model="preview.open" width="80%" top="5vh" append-to-body>
<el-tabs v-model="preview.activeName"> <el-tabs v-model="preview.activeName">
<el-tab-pane v-for="(item, key) in preview.data" :label="item.title" :id="key" :name="key.toString()" :key="key"> <el-tab-pane v-for="(item, key) in preview.data" :label="item.title" :id="key" :name="key.toString()" :key="key">
<!-- <el-link :underline="false" icon="DocumentCopy" v-copyText="item.content" class="btn-copy" <el-link :underline="false" icon="DocumentCopy" @click="onCopy(item.content)" class="btn-copy">复制 </el-link>
>复制
</el-link> -->
<pre><code class="hljs" v-html="highlightedCode(item.content, item.title)"></code></pre> <pre><code class="hljs" v-html="highlightedCode(item.content, item.title)"></code></pre>
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
@ -79,7 +77,7 @@ import { useRouter } from 'vue-router'
import importTable from './importTable' import importTable from './importTable'
import hljs from 'highlight.js' import hljs from 'highlight.js'
import 'highlight.js/styles/dark.css' // import 'highlight.js/styles/dark.css' //
import { useClipboard } from '@vueuse/core'
// const route = useRoute() // const route = useRoute()
const router = useRouter() const router = useRouter()
const { proxy } = getCurrentInstance() const { proxy } = getCurrentInstance()
@ -241,9 +239,15 @@ function highlightedCode(code, key) {
const result = hljs.highlightAuto(code || '') const result = hljs.highlightAuto(code || '')
return result.value || '&nbsp;' return result.value || '&nbsp;'
} }
/** 复制代码成功 */
function clipboardSuccess() { const { copy, isSupported } = useClipboard()
proxy.$modal.msgSuccess('复制成功') function onCopy(input) {
if (isSupported) {
copy(input)
proxy.$modal.msgSuccess('复制成功!')
} else {
proxy.$modal.msgError('当前浏览器不支持')
}
} }
getList() getList()
</script> </script>