vue-clipboard2在ios,mac的异步方法中无法copy的问题

时间:2021-07-30
本文章向大家介绍vue-clipboard2在ios,mac的异步方法中无法copy的问题,主要包括vue-clipboard2在ios,mac的异步方法中无法copy的问题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

需求是需要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.

上面已经明确说明这个方法无法用在异步方法里面(原因未知,得花点时间把源码研究一下)

喜欢艺术的码农

原文地址:https://www.cnblogs.com/zjhgx/p/15081222.html