Files
AgentCoord/frontend/src/ directive/devOnly/index.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

import type { Directive, DirectiveBinding } from 'vue'
import { useConfigStoreHook } from '@/stores'
/**
*
*
*
* \@param binding.value - true
* @example
* \!-- --
* \!div v-dev-only开发模式内容</div>
*
* \!-- --
* <div v-dev-only="true"></div>
* <div v-dev-only="false"></div>
*/
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)
}
}
}