[英]How to proper use clipboard.js within Nuxt?
I'm making my pet-project using Nuxt.js .我正在使用Nuxt.js制作我的宠物项目。 One of the small features I'd like to implement is copying some dynamic data to clipboard.我想实现的小功能之一是将一些动态数据复制到剪贴板。 For such tasks I have always used clipboard.js
, but never within Nuxt.js
.对于此类任务,我一直使用clipboard.js
,但从未在Nuxt.js
中使用。
Tthe solution I wrote is working, but I'm not sure that I used it proper way.我写的解决方案正在工作,但我不确定我是否以正确的方式使用它。
<button class="copyToClipboardBtn" data-clipboard-target="#output">Copy</button>
...
<textarea id="output">some dynamically generated text</textarea>
import ClipboardJS from "clipboard";
export default { // component
data() {
return {
clipboard: null, // data-property for storing clipboard instance
}
},
mounted() {
this.clipboard = new ClipboardJS('.copyToClipboardBtn');
},
destroyed() {
if ( this.clipboard ) {
this.clipboard.destroy();
}
},
}
So, my question is how to properly use such a libraries within Nuxt ?所以,我的问题是如何在 Nuxt 中正确使用这样的库?
I'm using Nuxt.js 2.14.7
and clipboard.js 2.0.6
( https://github.com/zenorocha/clipboard.js )我正在使用Nuxt.js 2.14.7
和clipboard.js 2.0.6
( https://github.com/zenorocha/clipboard.js )
Generally, for simplicity you would want to use a vue wrapper of this plugin and then import it as a nuxt plugin.通常,为简单起见,您会希望使用此插件的 vue 包装器,然后将其作为 nuxt 插件导入。
For example with this wrapper: https://github.com/Inndy/vue-clipboard2例如使用这个包装器: https://github.com/Inndy/vue-clipboard2
plugins/vueClipboard2.js
import Vue from 'vue'
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)
nuxt.config.js
export default {
// Some more config stuff
plugins: [
{ src: '~/plugins/vueClipboard2.js', mode: 'client' },
]
}
Then you are able to use it globally, usually under this.$something()
然后你就可以在全局范围内使用它了,通常在this.$something()
下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.