feat[SvgIcon]: support import svg from url (#2052)

This commit is contained in:
花裤衩 2019-05-20 11:03:29 +08:00 committed by GitHub
parent 4f620c4a0d
commit be3dfac13b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 1 deletions

View File

@ -1,10 +1,14 @@
<template> <template>
<svg :class="svgClass" aria-hidden="true" v-on="$listeners"> <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" />
<svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
<use :xlink:href="iconName" /> <use :xlink:href="iconName" />
</svg> </svg>
</template> </template>
<script> <script>
// doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage
import { isExternal } from '@/utils/validate'
export default { export default {
name: 'SvgIcon', name: 'SvgIcon',
props: { props: {
@ -18,6 +22,9 @@ export default {
} }
}, },
computed: { computed: {
isExternal() {
return isExternal(this.iconClass)
},
iconName() { iconName() {
return `#icon-${this.iconClass}` return `#icon-${this.iconClass}`
}, },
@ -27,6 +34,12 @@ export default {
} else { } else {
return 'svg-icon' return 'svg-icon'
} }
},
styleExternalIcon() {
return {
mask: `url(${this.iconClass}) no-repeat 50% 50%`,
'-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
}
} }
} }
} }
@ -40,4 +53,10 @@ export default {
fill: currentColor; fill: currentColor;
overflow: hidden; overflow: hidden;
} }
.svg-external-icon {
background-color: currentColor;
mask-size: cover!important;
display: inline-block;
}
</style> </style>