Vuejs从远程源获取图像

d5vmydt9  于 12个月前  发布在  Vue.js
关注(0)|答案(1)|浏览(87)

我使用下面的代码来检测按钮点击,并从unsplash中获取一个图像,它具有随机模式。
问题是它只工作一次,去获取一个随机的。但第二次还是没变。
它没有重新请求它,它只是没有改变它,因为它是相同的URL,我认为。

var vue = new Vue({
  el: '#app',
  data: {
    styleObject: {
        background: 'url(https://unsplash.it/1920/1080)'
    }
  },
  methods: {
    getImage: function() {
        vue.styleObject.background = 'url(https://unsplash.it/1920/1080?random)'
    }
  }
})

字符串

我试着擦除变量

getImage: function() {
    vue.styleObject.background = '';
    vue.styleObject.background = 'url(https://unsplash.it/1920/1080?random)'
}


但还是没有成功,有什么想法吗?

4nkexdtk

4nkexdtk1#

你可以尝试做一些缓存制动。我认为是您的浏览器缓存了请求并为您提供了相同的图像。尝试在请求的末尾附加一个随机字符串,如下所示:

methods: {
    getImage: function() {
        var max = 90000;
        var min = 10000;
        var slug = Math.random() * (max - min) + min;
        vue.styleObject.background = 'url(https://unsplash.it/1920/1080?random&s=' + slug +')';
    }
  }

字符串
希望这对你有帮助

相关问题