【发布时间】:2019-03-13 17:25:23
【问题描述】:
// Component.vue 带有 计算属性 - 不起作用
<template>
<section class="background" v-bind:style="{
'background-image': 'url(' + require(imageUrl) + ')'}"> .
</section>
</template>
<script>
export default {
computed: {
imageUrl() {
return './path/to/image';
}
}
}
</script>
// Component.vue - 直接方法有效
<template>
<section class="background" v-bind:style="{
'background-image': 'url(' + require('./path/to/image.jpg') + ')'}"> .
</section>
</template>
前一种实现的原因是我需要一个可以在每次重新加载时随机生成的计算属性。
这是计算属性案例的完整错误信息
[Vue warn]: Error in render: "Error: Cannot find module '../assets/media/images/1.jpg'"
found in
---> <Background>
<App> at src/App.vue
<Root>
warn @ vue.runtime.esm.js?2b0e:587
logError @ vue.runtime.esm.js?2b0e:1733
globalHandleError @ vue.runtime.esm.js?2b0e:1728
<stack trace> // I can not post the full message since it is not allowable by the policy of stackoverflow
【问题讨论】:
-
您尝试
return require('./path/to/image');时的结果如何? -
有效!我很惊讶。你能解释一下潜在的机制吗?我
-
我已发布为答案并添加了参考网页
标签: vue.js data-binding background-image render computed-properties