You need to enable JavaScript to run this app. Vue.js :class绑定时如何从Vimeo返回的tags数组提取canonical值而非键名
优惠活动
大模型
产品
解决方案
定价
更多

Vue.js :class绑定时如何从Vimeo返回的tags数组提取canonical值而非键名

解决方案

你直接将包含对象的video.tags数组传给:class时,Vue会遍历数组内对象的可枚举属性名作为类名,因此会输出name、canonical这类键名,而非对应属性值。

实现方式

直接通过数组的map方法遍历标签项,提取每个项的canonical属性值组成字符串数组,传给:class即可:

<figure class="vimeography-thumbnail" :class="video.tags.map(tag => tag.canonical)">

该写法会自动把所有标签的canonical值追加到静态类名之后,最终输出你期望的vimeography-thumbnail hands feet eyes格式。

完整修改后的模板代码

<script type="text/x-template" id="vimeography-timber-thumbnail">
    <figure class="vimeography-thumbnail" :class="video.tags.map(tag => tag.canonical)">    
    <img class="vimeography-thumbnail-img" :src="thumbnailUrl" :alt="video.name" />    
        <figcaption>      
            <h2 class="vimeography-title">{{video.name}}</h2>  
            <div class="vimeography-description" v-html="video.description"></div>  
            <router-link class="vimeography-link" :to="this.query" :title="video.name" exact exact-active-class="vimeography-link-active">查看更多</router-link>    
        </figcaption>  
    </figure>
</script>

如果模板环境不允许直接写map方法(部分低版本Vue定制场景限制),也可以提前在数据返回时预处理video.tags,将其转换为仅包含canonical值的数组再绑定,效果完全一致。

内容的提问来源于stack exchange,提问作者CK MacLeod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 23:15:05