我是 Vue 新手,做了一些 html 和 css,我想使用一个变量作为图像目录,但图像从未加载,该变量正在由一个有效的 tauri 函数更新,我需要图像也会改变。
这是我的一些代码
<template>
<img v-bind:src=getimg()>
-- and --
<img :src = {{data}}}>
-- and --
<img src = {{data}}>
-- and much more ... --
</template>
<script setup>
var data = ref("./assets/4168-376.png")
function getimg() {
console.log(data1.value)
return require(data1.value)
}
</setup>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
根据您的代码,您正在使用 Vue3 Composition API。您的代码中缺少一些内容,这些内容可能无法让您的应用正常运行。
ref等函数。<template> <img :src="data"> <img v-bind:src="data"> <img :src="getimg()"> </template> <script setup> import { ref } from 'vue' const data = ref("./assets/4168-376.png") // prefer const over var cus this variable will never be reassigned function getimg() { // why are you using data1.value tho? It should be data.value // also i don't think 'require' is needed here return require(data1.value) // i just copy paste from your code } </script>额外:当处理不需要参数的值时,通常使用
计算会更好。请参阅 Vue 计算属性<template> <img :src="data"> <img v-bind:src="data"> <img :src="getImg"> </template> <script setup> import { ref, computed } from 'vue' // import it first const data = ref("./assets/4168-376.png") const getImg = computed(() => { return data.value })