【发布时间】:2021-12-09 20:27:18
【问题描述】:
我想使用:
homePage.image = 'storage/' + 'rkiGXBj7KJSOtsR5jiYTvNOajnzo7MlRAoXOXe3V.jpg'
内部:
<div class="home-image" :style="{'background-image': 'url(' + homePage.image + ')'}"></div>
在渲染 Vue 组件之前,但 homePage.image 返回 ""。
这是我的组件:
<template>
<section id="home" class="home">
<div class="image-container">
<div class="home-image" :style="{'background-image': 'url(' + homePage.image + ')'}"></div>
</div>
</section>
</template>
<script setup>
import {ref} from 'vue';
const homePage = ref({
image: ""
});
axios.get('/home')
.then(res => {
homePage.image = 'storage/' + 'rkiGXBj7KJSOtsR5jiYTvNOajnzo7MlRAoXOXe3V.jpg'; // storage link + image file name
})
.catch(err => {
console.log(err.response);
});
// works
// const image = 'storage/' + 'rkiGXBj7KJSOtsR5jiYTvNOajnzo7MlRAoXOXe3V.jpg';
</script>
如何让homePage.image 在从axios 函数内部加载到组件中之前更新为'storage/' + 'rkiGXBj7KJSOtsR5jiYTvNOajnzo7MlRAoXOXe3V.jpg'?
【问题讨论】:
标签: ajax vue.js axios vuejs3 vue-composition-api