如果通过get赋值url, http://localhost:8081/img/AllCommodity则图片不出现,但是如果提前写死url路径为 http://localhost:8081/img/AllCommodity则出现,是为什么
时间: 2023-08-14 09:08:40 浏览: 33
如果你使用 `get` 方法获取数据并将 `imageSrc` 赋值为 `http://localhost:8081/img/AllCommodity/${url}`,但是图片无法正常显示,可能是因为你在使用 `get` 方法获取数据时,数据还没有完全加载完成,导致模板中的 `img` 标签中的 `src` 属性值为 `http://localhost:8081/img/AllCommodity/`,而变量 `url` 的值还未被成功赋值。
为了避免这种情况,你可以在获取数据成功之后,再将 `http://localhost:8081/img/AllCommodity/${url}` 赋值给 `imageSrc`,或者在 `imageSrc` 中直接使用完整的图片路径。
以下是两种解决方法:
1. 在获取数据成功之后,再将 `http://localhost:8081/img/AllCommodity/${url}` 赋值给 `imageSrc`。
```
<template>
<div>
<img :src="imageSrc" />
</div>
</template>
<script>
export default {
data() {
return {
imageSrc: '',
};
},
async created() {
const response = await axios.get('your-api-url');
const url = response.data.url; // 假设获取到的数据格式为 { url: 'example.jpg' }
this.imageSrc = `http://localhost:8081/img/AllCommodity/${url}`;
},
};
</script>
```
在这个例子中,我们在获取数据成功之后,将 `http://localhost:8081/img/AllCommodity/${url}` 赋值给了 `imageSrc`。这样在模板中使用时,就不会出现无法正常显示图片的问题了。
2. 在 `imageSrc` 中直接使用完整的图片路径。
```
<template>
<div>
<img :src="`http://localhost:8081/img/AllCommodity/${url}`" />
</div>
</template>
<script>
export default {
data() {
return {
url: 'example.jpg',
};
},
};
</script>
```
在这个例子中,我们在 `imageSrc` 中直接使用了完整的图片路径。这样在模板中使用时,也不会出现无法正常显示图片的问题了。
希望这些解决方法可以帮助你解决问题。如果问题仍然存在,请提供更多详细信息,我会尽力帮助你解决问题。