VueJS how to prevent all anchors with # values to reload

Viewed 22

I have <a href='#section'> html injected on a div.

<div v-html="content"></div>

Every time the anchor is click, the page reload.

How can I prevent this from reload but still redirects to the section.

Thank you

2 Answers

try this

<a href="#section" @click="$event.preventDefault()">

Add an Updated hook to your component. In that hook create a new on click handler and call event.preventDefault()

Something like:

{
    Updated: () => {
       document.querySelectorAll("div a[href='#']").addEventListener("click", event => event.preventDefault())
    }
}  
Related