You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue中v-if如何实现判断变量相等或包含指定字符#的需求

解决方案

判断字符串是否包含指定字符可以使用 JavaScript 自带的includes()方法,把原来判断全等#的逻辑替换为包含判断即可,以下是对应场景的可用代码:

场景1:两个条件同时满足才展示(current_variant.alt 既等于 media.alt,同时自身包含#字符)

<div v-if="current_variant.alt == '{{ media.alt | escape }}' && current_variant.alt.includes('#')">

</div>

场景2:两个条件满足任意一个就展示(要么current_variant.alt等于media.alt,要么自身包含#字符)

<div v-if="current_variant.alt == '{{ media.alt | escape }}' || current_variant.alt.includes('#')">

</div>

注:追加了Liquid的escape过滤器处理media.alt的输出,避免alt值包含单引号时导致JS语法报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 17:54:04