Vue.js 中使用 prop 设置 img 标签宽度并处理空值
<template>
<div>
<img v-bind:src='imageUrl' v-bind:width='imageWidth || '100%'' />
</div>
</template>
<script>
export default {
props: {
imageUrl: String,
imageWidth: {
type: [String, Number],
default: null,
},
},
};
</script>
<p>在上面的代码中,我们定义了一个 props 属性 imageWidth,它可以是字符串或数字类型,默认值为 null。在 img 标签中,我们使用 v-bind 指令将 props 属性 imageWidth 绑定到 width 属性上,同时使用逻辑或运算符(||)来设置默认值为 100%。这样,如果 imageWidth 的值为 null 或 undefined,img 标签的 width 属性就会被设置为 100%。</p>
原文地址: https://www.cveoy.top/t/topic/lDOi 著作权归作者所有。请勿转载和采集!