限定名称
在使用svg时,有时候会遇到写入名称错误的问,为了避免这种情况,在ts中我们可以用type类型限定name的名称,这里注意文件夹要保持与iconName相同(重在练习ts)
icon.tsx
import { defineComponent, defineProps, PropType } from 'vue';
import s from './Icon.module.scss';
export type IconName = 'add' | 'chart' | 'clock' | 'cloud' |
'mangosteen' | 'pig' | 'menu' | 'charts' | 'notify' | 'export' | 'left' | 'notes' | 'date'|'none'|'cai'
export const Icon = defineComponent({
props: {
name: {
type: String as PropType<IconName>,
required: true,
},
onClick: {
type: Function as PropType<(e: MouseEvent) => void>
}
},
setup: (props, context) => {
return () => (
<svg class={s.icon} onClick={props.onClick}>
<use xlinkHref={'#' + props.name}></use>
</svg>
)
}
})