我试图在我的v-for循环中为每个项目获取不同的视频,但它不起作用。
<section v-if="loaded" class="port-content">
<div class="item-box reveal" v-for="item in this.items" v-bind:key="item.title">
...
<div class="item-vid-wrapper">
<video controls>
<source :src="item.vidPath" type="video/mp4">
</video>
</div>
</div>
</section>
项目中的vidPath变数是'../assets/homeVid.mp4'。
通常,在src元素中设置路径会从这个.vue文件所在的文件夹开始。因此,如果我的路径是..“/assets/homeVid.mp4”,它会转到父文件夹并进入assets文件夹。这成功地将homeVid.mp4视频设置为所有的v-for元素:
<source src="../assets/homeVid.mp4" type="video/mp4">
当我尝试使用item.vidPath动态设置它时,它似乎向localhost发出GET请求,忽略'..',并尝试从那里找到视频:
如何将item.vidPath分配给video src,就像我对路径进行硬编码一样?
1条答案
按热度按时间pcww981p1#
想通了!
如果我将项目.vidPath设置为“@/assets/homeVid.mp4”并执行以下操作,则无法正常工作:
但是,如果item.vidPath正好等于'homeVid.mp4',并且字符串的开头是硬编码的,则它可以工作:
因此,在v-for循环中,它看起来像这样: