注册全局组件SVG,集成pinia,封装axios
This commit is contained in:
19
frontend/src/components/index.ts
Normal file
19
frontend/src/components/index.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { App, Component } from 'vue'
|
||||
|
||||
// 当组件很多的时候,可以使用
|
||||
import { SvgIcon } from './staticExtend/SvgIcon'
|
||||
|
||||
// 这个地方将合并到对象中
|
||||
const Components: {
|
||||
[propName: string]: Component
|
||||
} = { SvgIcon }
|
||||
|
||||
// 批量注册全局组件
|
||||
export default {
|
||||
install: (app: App) => {
|
||||
Object.keys(Components).forEach((key) => {
|
||||
app.component(key, Components[key])
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
2
frontend/src/components/staticExtend/SvgIcon/index.ts
Normal file
2
frontend/src/components/staticExtend/SvgIcon/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
import SvgIcon from './src/SvgIcon.vue'
|
||||
export { SvgIcon }
|
||||
89
frontend/src/components/staticExtend/SvgIcon/src/SvgIcon.vue
Normal file
89
frontend/src/components/staticExtend/SvgIcon/src/SvgIcon.vue
Normal file
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* @name SvgIcon
|
||||
* @description svg图标组件
|
||||
* 支持定义名称、颜色、大小、旋转
|
||||
* @example <SvgIcon name="icon-name" color="#fff" size="20" spin />
|
||||
*/
|
||||
<template>
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
:class="['svg-icon', spin && 'svg-icon-spin']"
|
||||
:style="getStyle"
|
||||
>
|
||||
<use :xlink:href="symbolId" :fill="color" />
|
||||
</svg>
|
||||
</template>
|
||||
<script setup lang='ts'>
|
||||
defineOptions({
|
||||
name: 'SvgIcon',
|
||||
})
|
||||
import { computed } from 'vue'
|
||||
import type { CSSProperties } from 'vue'
|
||||
// 定义组件对外暴露的props
|
||||
const props = defineProps({
|
||||
prefix: {
|
||||
type: String,
|
||||
default: 'icon',
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
color: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
size: {
|
||||
type: [Number, String],
|
||||
default: 20,
|
||||
},
|
||||
spin: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
})
|
||||
|
||||
//计算属性获取symbolId
|
||||
const symbolId = computed(() => {
|
||||
return `#${props.prefix}-${props.name}`
|
||||
})
|
||||
|
||||
//计算属性获取svg样式
|
||||
const getStyle = computed((): CSSProperties => {
|
||||
const { size } = props
|
||||
let s = `${size}`
|
||||
// 确保size为px单位
|
||||
s = `${s.replace('px', '')}px`
|
||||
return {
|
||||
width: s,
|
||||
height: s,
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
</script>
|
||||
<style scoped>
|
||||
|
||||
.svg-icon {
|
||||
display: inline-block;
|
||||
overflow: hidden;
|
||||
vertical-align: -0.15em;
|
||||
fill: currentColor;
|
||||
}
|
||||
|
||||
.svg-icon-spin {
|
||||
animation: loadingCircle 1.2s infinite linear;
|
||||
}
|
||||
|
||||
/* 旋转动画 */
|
||||
@keyframes loadingCircle {
|
||||
0% {
|
||||
transform: rotate(0);
|
||||
}
|
||||
100% {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
</style>
|
||||
Reference in New Issue
Block a user