vue3 composition api script setup, share click event with other components

huangapple go评论44阅读模式
英文:

vue3 composition api script setup, share click event with other components

问题

我正在使用Vue3(使用script setup的组合式API),尝试与其他组件共享点击事件。

<h1 @click="useLink">Header #</h1>




```
		const useLink = (e) =&gt; {
			let section = e.target.closest(&quot;.section&quot;);
	
			if (router.currentRoute.value.hash !== &quot;#&quot; + section.id) {
				router.push(&quot;#&quot; + section.id);
			}
		};
```
注意:这个方法在其他几个组件中重复出现。 ☹️



当用户点击h1时,会调用useLink(),并将该id推送到路由器,从而滚动到相应位置。


谢谢
```

<details>
<summary>英文:</summary>

I am using Vue3 (composition API with script setup) and I am trying to share a click event with other components.

```
&lt;h1 @click=&quot;useLink&quot;&gt;Header #&lt;/h1&gt;
		const useLink = (e) =&gt; {
			let section = e.target.closest(&quot;.section&quot;);
	
			if (router.currentRoute.value.hash !== &quot;#&quot; + section.id) {
				router.push(&quot;#&quot; + section.id);
			}
		};

Note: This method is repeated in few other components. ☹️

When a user clicks on h1, useLink() gets called and pushes that id to router which scrolls to position.

Thanks

答案1

得分: 1

link.js

import { useRouter } from 'vue-router';

export function useLink() {
  const router = useRouter();

  function goToSection(e) {
    const section = e.target.closest('.section');

    if (router.currentRoute.value.hash !== '#' + section.id) {
      router.push('#' + section.id);
    }
  }
  return {
    goToSection
  };
}

component.vue

<script setup>
import { useLink } from '@/composables/link';

const { goToSection } = useLink();
</script>
<template>
  <div class="section" id="one">
    .
    .
    .
    <h1 @click="goToSection">Scroll to section div</h1>
  </div>
</template>
英文:

A composable should export a function that returns some reusable logic (in this case, another function)

link.js

import { useRouter } from &#39;vue-router&#39;;

export function useLink() {
  const router = useRouter();

  function goToSection(e) {
    const section = e.target.closest(&#39;.section&#39;);

    if (router.currentRoute.value.hash !== &#39;#&#39; + section.id) {
      router.push(&#39;#&#39; + section.id);
    }
  }
  return {
    goToSection
  };
}

Then in any component you need this reusable function: import the file, destructure the reusable function, and apply it to your @click handlers.

component.vue

&lt;script setup&gt;
import { useLink } from &#39;@/composables/link&#39;;

const { goToSection } = useLink();
&lt;/script&gt;
&lt;template&gt;
  &lt;div class=&quot;section&quot; id=&quot;one&quot;&gt;
    .
    .
    .
    &lt;h1 @click=&quot;goToSection&quot;&gt;Scroll to section div&lt;/h1&gt;
  &lt;/div&gt;
&lt;/template&gt;

huangapple
  • 本文由 发表于 2023年2月8日 23:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定