From 46766a54fb4ae94bcc6ed06c880e4f1f92061dc9 Mon Sep 17 00:00:00 2001 From: chansee97 Date: Mon, 10 Jun 2024 12:38:27 +0800 Subject: [PATCH] feat: local svg icon --- src/components/common/IconSelect.vue | 50 +++++++++++++++++++++--- src/components/common/NovaIcon.vue | 32 ++++++++++++--- src/layouts/components/header/Search.vue | 4 +- src/router/routes.static.ts | 2 +- src/store/router/index.ts | 7 +++- src/utils/icon.ts | 30 +++++++++++++- 6 files changed, 108 insertions(+), 17 deletions(-) diff --git a/src/components/common/IconSelect.vue b/src/components/common/IconSelect.vue index 8629859..a98a07d 100644 --- a/src/components/common/IconSelect.vue +++ b/src/components/common/IconSelect.vue @@ -1,4 +1,6 @@ diff --git a/src/layouts/components/header/Search.vue b/src/layouts/components/header/Search.vue index 04d7f09..acea1d4 100644 --- a/src/layouts/components/header/Search.vue +++ b/src/layouts/components/header/Search.vue @@ -183,7 +183,9 @@ function handleMouseEnter(index: number) { @mousemove="setKeyboardFalse" >
- +
+ +
{{ option.label }} {{ option.value }} diff --git a/src/router/routes.static.ts b/src/router/routes.static.ts index 2036244..3418296 100644 --- a/src/router/routes.static.ts +++ b/src/router/routes.static.ts @@ -208,7 +208,7 @@ export const staticRoutes: AppRoute.RowRoute[] = [ path: '/demo/icons', title: '图标', requiresAuth: true, - icon: 'icon-park-outline:winking-face-with-open-eyes', + icon: 'local:cool', componentPath: '/demo/icons/index.vue', id: 22, pid: 13, diff --git a/src/store/router/index.ts b/src/store/router/index.ts index e94892d..ac1908b 100644 --- a/src/store/router/index.ts +++ b/src/store/router/index.ts @@ -1,4 +1,4 @@ -import { createMenus, createRoutes } from './helper' +import { createMenus, createRoutes, generateCacheRoutes } from './helper' import { $t, local } from '@/utils' import { router } from '@/router' import { fetchUserRoutes } from '@/service' @@ -16,9 +16,9 @@ export const useRouteStore = defineStore('route-store', { state: (): RoutesStatus => { return { isInitAuthRoute: false, + activeMenu: null, menus: [], rowRoutes: [], - activeMenu: null, cacheRoutes: [], } }, @@ -79,6 +79,9 @@ export const useRouteStore = defineStore('route-store', { // Generate side menu this.menus = createMenus(rowRoutes) + // Generate the route cache + this.cacheRoutes = generateCacheRoutes(rowRoutes) + this.isInitAuthRoute = true }, }, diff --git a/src/utils/icon.ts b/src/utils/icon.ts index cfccc55..4f779a6 100644 --- a/src/utils/icon.ts +++ b/src/utils/icon.ts @@ -1,6 +1,32 @@ import { Icon } from '@iconify/vue' import { NIcon } from 'naive-ui' -export function renderIcon(icon: string, props?: import('naive-ui').IconProps) { - return () => h(NIcon, props, { default: () => h(Icon, { icon }) }) +export function renderIcon(icon?: string, props?: import('naive-ui').IconProps) { + if (!icon) + return + + return () => createIcon(icon, props) +} + +export function createIcon(icon?: string, props?: import('naive-ui').IconProps) { + if (!icon) + return + + const isLocal = icon.startsWith('local:') + let innerIcon: any + if (isLocal) { + const svgName = icon.replace('local:', '') + const svg = import.meta.glob('@/assets/svg-icons/*.svg', { + query: '?raw', + import: 'default', + eager: true, + }) + const target = svg[`/src/assets/svg-icons/${svgName}.svg`] + innerIcon = h(NIcon, { ...props, innerHTML: target }) + } + else { + innerIcon = h(NIcon, props, { default: () => h(Icon, { icon }) }) + } + + return innerIcon }