From c30e9197684e185090dbcfe4e8ae26733ba5ff57 Mon Sep 17 00:00:00 2001 From: AC-0308 Date: Mon, 15 Dec 2025 18:40:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(OAnchor):=20=E2=9C=A8=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?observeHref=E5=8F=82=E6=95=B0=EF=BC=8C=E4=BB=A5=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=9B=91=E5=90=AC=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../opendesign/src/anchor/OAnchorItem.vue | 20 ++++++++++--------- packages/opendesign/src/anchor/types.ts | 7 +++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/opendesign/src/anchor/OAnchorItem.vue b/packages/opendesign/src/anchor/OAnchorItem.vue index c069fb20..2f5eb44c 100644 --- a/packages/opendesign/src/anchor/OAnchorItem.vue +++ b/packages/opendesign/src/anchor/OAnchorItem.vue @@ -21,22 +21,24 @@ const emits = defineEmits<{ const anchorInjection = inject(anchorInjectKey, null); const anchorItemInjection = inject(anchorItemInjectKey, null); +const _observeHref = computed(() => props.observeHref || props.href) + const isActive = computed(() => { - return props.href === anchorInjection?.activeLink.value; + return _observeHref.value === anchorInjection?.activeLink.value; }); const addItem = () => { - if (!props.href) { + if (!_observeHref.value) { return; } - anchorInjection?.addLink(props.href); + anchorInjection?.addLink(_observeHref.value); }; const removeItem = () => { - if (!props.href) { + if (!_observeHref.value) { return; } - anchorInjection?.removeLink(props.href); + anchorInjection?.removeLink(_observeHref.value); }; const onClick = (event: MouseEvent) => { @@ -60,15 +62,15 @@ const onClick = (event: MouseEvent) => { */ anchorInjection?.onItemClick({ event: event, - link: props.href, + link: _observeHref.value, }); - if (props.href) { - anchorInjection?.scrollIntoView(props.href); + if (_observeHref.value) { + anchorInjection?.scrollIntoView(_observeHref.value); } }; watch( - () => props.href, + () => _observeHref.value, (newVal, oldVal) => { nextTick(() => { if (oldVal) { diff --git a/packages/opendesign/src/anchor/types.ts b/packages/opendesign/src/anchor/types.ts index 862fb67f..4c16b5ab 100644 --- a/packages/opendesign/src/anchor/types.ts +++ b/packages/opendesign/src/anchor/types.ts @@ -68,6 +68,13 @@ export const anchorItemProps = { type: String, required: true, }, + /** + * @zh-CN 锚点监听的目标元素(带#前缀),不传时监听href + * @en-US Target element for anchor observe (with # prefix),Use href prop by default + */ + observeHref: { + type: String, + }, /** * @zh-CN 锚点跳转方式 * @en-US Anchor navigation method -- Gitee