diff --git a/frontend/public/config.json b/frontend/public/config.json index fc2bd32..fc6606d 100644 --- a/frontend/public/config.json +++ b/frontend/public/config.json @@ -15,5 +15,6 @@ ], "agentRepository": { "storageVersionIdentifier": "1" - } + }, + "dev": true } diff --git a/frontend/src/ directive/devOnly/index.ts b/frontend/src/ directive/devOnly/index.ts new file mode 100644 index 0000000..0f20554 --- /dev/null +++ b/frontend/src/ directive/devOnly/index.ts @@ -0,0 +1,43 @@ +import type { Directive, DirectiveBinding } from 'vue' +import { useConfigStoreHook } from '@/stores' + +/** + * 开发模式专用指令 + * 只在开发模式下显示元素,生产模式下会移除该元素 + * + * \@param binding.value - 是否开启该功能,默认为 true + * @example + * \!-- 默认开启,开发模式显示 -- + * \!div v-dev-only开发模式内容 + * + * \!-- 传入参数控制 -- + *
开启指令
+ *
取消指令
+ */ +export const devOnly: Directive = { + mounted(el: HTMLElement, binding: DirectiveBinding) { + checkAndRemoveElement(el, binding) + }, + + updated(el: HTMLElement, binding: DirectiveBinding) { + checkAndRemoveElement(el, binding) + }, +} + +const configStore = useConfigStoreHook() + +/** + * 检查并移除元素的逻辑 + */ +function checkAndRemoveElement(el: HTMLElement, binding: DirectiveBinding) { + const isDev = typeof configStore.config.dev === 'boolean' ? configStore.config.dev : import.meta.env.DEV + // 默认值为 true,如果没有传值或者传值为 true 都启用 + const shouldEnable = binding.value !== false + // 如果不是开发模式或者明确禁用,移除该元素 + if (!isDev && shouldEnable) { + console.log(1111) + if (el.parentNode) { + el.parentNode.removeChild(el) + } + } +} diff --git a/frontend/src/ directive/index.ts b/frontend/src/ directive/index.ts new file mode 100644 index 0000000..df63de2 --- /dev/null +++ b/frontend/src/ directive/index.ts @@ -0,0 +1,9 @@ +import type { App } from 'vue' + +import { devOnly } from './devOnly' + + +// 全局注册 directive +export function setupDirective(app: App) { + app.directive('dev-only', devOnly) +} diff --git a/frontend/src/App.vue b/frontend/src/App.vue index ab89ddd..bf7c7e9 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -3,7 +3,7 @@ import Layout from './layout/index.vue'