需求是需要copy从后台接口返回的字符串到剪切板,在mac 的chrome,android 微信,浏览器中测试都可以,但在iphone safari,微信,还有mac 的safari中copy都失败
this.$axios.get(`${this.host}/goods/go/${code}`).then((res) => { console.log(‘res = ‘ + res.data); this.taobaoPwd = res.data; console.log(‘taobaoPwd = ‘ + res.data); let that = this; this.$copyText(this.taobaoPwd).then( function (e) { console.log(e); that.showing = true; }, function (e) { alert(‘Can not copy‘); console.log(e); }, );
因为this.$copyText是在$axios这个异步方法里的,而在vue-clipboard2主页上有一句话,[https://github.com/Inndy/vue-clipboard2]
Yes, you can do it by using our new method: this.$copyText. See sample2, where we replace the clipboard directives with a v-on directive.
Modern browsers have some limitations like that you can‘t use window.open without a user interaction. So there‘s the same restriction on copying things! Test it before you use it.
Make sure you are not using this method inside any async method.
上面已经明确说明这个方法无法用在异步方法里面(原因未知,得花点时间把源码研究一下)