vue中经常要用到的代码

洛飞 / 2024-10-13 / 原文

  • 属性使用动态值加固定值用
:a="`${变量名}` + '固定值'"

例如:

<svg :style="{width,height}">
    <!-- 内部需要用use结合使用 -->
    <use :xlink:href="`${prefix}` + 'mao'" :fill="color"></use>
    <!-- <use xlink:href="#icon-surf"></use> -->
</svg>
  •  给style标签设置动态值
:style="{属性名:变量名}"

例如:

<li :style="{color: item.done ? 'green' : 'red'}">
  • defineProps位变量名设置类型和默认值
defineProps({
   变量名:{
       type:变量类型,
       default:默认值  
    } 
})

例如:

defineProps({
    prefix: {
        type: String,
        default: "#icon-",
    },
    iconName:String,
    color:{
        type:String,
    },
});