VueJS使用来自数据对象的图像

4bbkushb  于 2023-02-05  发布在  Vue.js
关注(0)|答案(4)|浏览(117)

我把一个公益网站的基础上,引导机构模板的朋友。
我在portfolio部分,我创建了Portfolio.vue组件,除了从组件内部的数据动态加载图像外,其他都可以正常工作。
例如:工作正常。
但是如果我尝试执行:src={{obj.img}},我会得到一个错误。
下面是代码.动态图像在VueJS中让我抓狂.
谢谢

<template>
            <section class="page-section bg-light" id="portfolio">
            <div class="container">
                <div class="text-center">
                    <h2 class="section-heading text-uppercase">Portfolio</h2>
                    <h3 class="section-subheading text-muted">Lorem ipsum dolor sit amet consectetur.</h3>
                </div>
                <div class="row">
                    <div class="col-lg-4 col-sm-6 mb-4" v-for="(obj, key) in portfolioJSON" :key="key">
                        <div class="portfolio-item">
                            <a class="portfolio-link" data-toggle="modal" href="#portfolioModal1">
                                <div class="portfolio-hover">
                                    <div class="portfolio-hover-content"><i class="fas fa-plus fa-3x"></i></div>
                                </div>
                                <img class="img-fluid" src="../assets/img/portfolio/01-thumbnail.jpg" alt="" />
                            </a>
                            <div class="portfolio-caption">
                                <div class="portfolio-caption-heading">{{obj.caption}}</div>
                                <div class="portfolio-caption-subheading text-muted">{{obj.title}}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
</template>

<script>
export default {
    data: () => ({
        portfolioJSON: [
            { 
                img: '../assets/img/portfolio/01-thumbnail.jpg',
                caption: 'Threads',
                title: 'Illustration'
            },
            { 
                img: '../assets/img/portfolio/02-thumbnail.jpg',
                caption: 'Explore',
                title: 'Graphic Design'
            },
            { 
                img: '../assets/img/portfolio/03-thumbnail.jpg',
                caption: 'Finish',
                title: 'Identity'
            },
            { 
                img: '../assets/img/portfolio/04-thumbnail.jpg',
                caption: 'Lines',
                title: 'Branding'
            },           
            { 
                img: '../assets/img/portfolio/05-thumbnail.jpg',
                caption: 'Southwest',
                title: 'Website Design'
            },
            { 
                img: '../assets/img/portfolio/06-thumbnail.jpg',
                caption: 'Window',
                title: 'Photography'
            }
        ]
    })
}
</script>

<style scoped>

</style>
6jjcrrmo

6jjcrrmo1#

谢谢大家。
上面的答案让我走上了寻找解决方案的道路。经过大量的挖掘,这是我想出的...

<img class="img-fluid" :src="require(`@/assets/img/portfolio/${obj.img}`)" alt="">

下面是完整的代码...

<template>
            <section class="page-section bg-light" id="portfolio">
            <div class="container">
                <div class="text-center">
                    <h2 class="section-heading text-uppercase">Portfolio</h2>
                    <h3 class="section-subheading text-muted">Lorem ipsum dolor sit amet consectetur.</h3>
                </div>
                <div class="row">
                    <div class="col-lg-4 col-sm-6 mb-4" v-for="(obj, key) in portfolioJSON" :key="key">
                        <div class="portfolio-item">
                            <a class="portfolio-link" data-toggle="modal" href="#portfolioModal1">
                                <div class="portfolio-hover">
                                    <div class="portfolio-hover-content"><i class="fas fa-plus fa-3x"></i></div>
                                </div>
                                <img class="img-fluid" :src="require(`@/assets/img/portfolio/${obj.img}`)" alt="">
                            </a>
                            <div class="portfolio-caption">
                                <div class="portfolio-caption-heading">{{obj.caption}}</div>
                                <div class="portfolio-caption-subheading text-muted">{{obj.title}}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
</template>

<script>
export default {
    data: () => ({
        portfolioJSON: [
            { 
                img: '01-thumbnail.jpg',
                caption: 'Threads',
                title: 'Illustration'
            },
            { 
                img: '02-thumbnail.jpg',
                caption: 'Explore',
                title: 'Graphic Design'
            },
            { 
                img: '03-thumbnail.jpg',
                caption: 'Finish',
                title: 'Identity'
            },
            { 
                img: '04-thumbnail.jpg',
                caption: 'Lines',
                title: 'Branding'
            },           
            { 
                img: '05-thumbnail.jpg',
                caption: 'Southwest',
                title: 'Website Design'
            },
            { 
                img: '06-thumbnail.jpg',
                caption: 'Window',
                title: 'Photography'
            }
        ]
    })
}
</script>

<style scoped>

</style>
vd2z7a6w

vd2z7a6w2#

去掉大括号,然后尝试:src="obj.img"
不能在HTML属性内使用胡须。请改用v-bind指令:
<div v-bind:id="dynamicId"></div>
https://v2.vuejs.org/v2/guide/syntax.html#Attributes

nfeuvbwi

nfeuvbwi3#

使用相对路径在你的数据对象将不再工作的这大部分时间因为vue开始查找从这主要项目目录.开始从你的源文件夹象这为我工作.

./src/assets/img/portfolio/01-thumbnail.jpg

试试看

ryoqjall

ryoqjall4#

<script>
export default {
    data: () => ({
        portfolioJSON: [
            { 
                img: require('@assets/img/portfolio/01-thumbnail.jpg'),
                caption: 'Threads',
                title: 'Illustration'
            },
            { 
                img: require('@assets/img/portfolio/02-thumbnail.jpg'),
                caption: 'Explore',
                title: 'Graphic Design'
            },
            { 
                img: require('@assets/img/portfolio/03-thumbnail.jpg'),
                caption: 'Finish',
                title: 'Identity'
            },
            { 
                img: require('@assets/img/portfolio/04-thumbnail.jpg'),
                caption: 'Lines',
                title: 'Branding'
            },           
            { 
                img: require('@assets/img/portfolio/05-thumbnail.jpg'),
                caption: 'Southwest',
                title: 'Website Design'
            },
            { 
                img: require('@assets/img/portfolio/06-thumbnail.jpg'),
                caption: 'Window',
                title: 'Photography'
            }
        ]
    })
}
</script>
<template>
            <section class="page-section bg-light" id="portfolio">
            <div class="container">
                <div class="text-center">
                    <h2 class="section-heading text-uppercase">Portfolio</h2>
                    <h3 class="section-subheading text-muted">Lorem ipsum dolor sit amet consectetur.</h3>
                </div>
                <div class="row">
                    <div class="col-lg-4 col-sm-6 mb-4" v-for="(obj, key) in portfolioJSON" :key="key">
                        <div class="portfolio-item">
                            <a class="portfolio-link" data-toggle="modal" href="#portfolioModal1">
                                <div class="portfolio-hover">
                                    <div class="portfolio-hover-content"><i class="fas fa-plus fa-3x"></i></div>
                                </div>
                                <img class="img-fluid" :src="obj.img" alt="" />
                            </a>
                            <div class="portfolio-caption">
                                <div class="portfolio-caption-heading">{{obj.caption}}</div>
                                <div class="portfolio-caption-subheading text-muted">{{obj.title}}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
</template>

相关问题