写一个Vue3随机更换必应壁纸背景
时间: 2023-03-16 16:43:49 浏览: 198
答:这里是一个使用Vue3随机更换必应壁纸背景的示例代码:// 获取图片地址const getImageUrl = async () => {const response = await fetch('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');const json = await response.json();return `https://www.bing.com${json.images[0].url}`;};// 设置图片为背景const setBackgroundImage = async () => {const url = await getImageUrl();document.body.style.backgroundImage = `url('${url}')`;};// 执行setBackgroundImage();
相关问题
写一个Vue3的随机必应壁纸背景
我可以给你一些Vue3代码来实现一个随机必应壁纸背景:<template>
<div>
<div :style="{ backgroundImage: 'url(' + bingImg + ')' }"></div>
</div>
</template><script>
export default {
data() {
return {
bingImg: ''
};
},
mounted() {
fetch('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1')
.then(response => response.json())
.then(data => {
this.bingImg = `https://www.bing.com${data.images[0].url}`;
});
}
};
</script>
阅读全文