简体   繁体   English

Vue.js导入图像

[英]Vue.js import images

So I am making an app with a control panel and i want to be able to change a few images inside the app dynamically and my problem is, it just won't work 所以我正在制作一个带有控制面板的应用程序,我希望能够动态更改应用程序内的一些图像,但我的问题是,它根本无法工作

This is the div I am trying to change 这是我要更改的div

<div class="bg-image bg-parallax overlay" :style="`background-image:url(${bg1url})`"></div>

this is the script part of the Home.vue file 这是Home.vue文件的脚本部分

<script>
import axios from 'axios';
export default {
    name: 'Home', //  this is the name of the component
    data () {
        return{
            page_data: {},
            bg1url: null,
        };
    },
    created() {
        axios.get("http://localhost:5001/api/v1/pages")
            .then((result) => {this.page_data = result.data.page_data});
        this.bg1url = require('@/assets/img/' + this.page_data.background1);
        alert(page_data.background1);
    },
};
</script>

I have tried most of the suggestions on stack overflow but nothing seems to work. 我已经尝试了关于堆栈溢出的大多数建议,但似乎没有任何效果。 I use the default webpack configurations and generated structure 我使用默认的webpack配置和生成的结构

Note: the parts with axios fetching from the backend work correctly. 注意:从后端获取axios的部件可以正常工作。 The only problem is adding the image to the style. 唯一的问题是将图像添加到样式中。

I think could be because you are setting the value for bg1url outsite of promise (calback function of axios), and so this make the code sync and not async 我认为可能是因为您正在设置bg1url的值超出promise(axios的回调功能),所以这会使代码同步而不是异步

so please try to update, use this instead 所以请尝试更新,改用它

created() {
    axios.get("http://localhost:5001/api/v1/pages").then(result => {
        this.page_data = result.data.page_data
        this.bg1url = require('@/assets/img/' + this.page_data.background1);
    });
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM