这节课,我们来看下,怎么将一个 props 限制在一个特定的值的集合中。
假设,我们有一个 Image 组件,内容如下:
<template>
<img
class="image"
:class="computeImgStyle"
src="https://gimg2.baidu.com/image_search/src=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttp%2Fnews.yxrb.net%2Fuploadfile%2F2021%2F0419%2F20210419103029168.jpg&refer=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttp%2Fnews.yxrb.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661221742&t=07639ae48c1b346e8beaca365ce0b1c1"
/>
</template>
<script setup>
import { ref, computed } from 'vue'
const props = defineProps({
imgStyle: {
type: String,
default: 'square',
},
})
const computeImgStyle = computed(() => {
return {
square: props.imgStyle === 'square',
rounded: props.imgStyle === 'rounded',
}
})
</script>
<styl